I need to display muiltiple markers on my home page on a map. I;am storing each Travel Flyers with a lng and lat in the database, then I display it for each flyer. (Which works fine), but when I display it on my home page with this code, it only displays the last lat and lng that was inserted into the database.
我需要在地图上的主页上显示多个标记。我;我将每个旅行传单与lng和lat存储在数据库中,然后我为每个传单显示它。 (哪个工作正常),但是当我使用此代码在我的主页上显示它时,它只显示插入数据库的最后一个lat和lng。
Here is my code:
这是我的代码:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBaB_9hgeARViVKIT6O1pFKXRCSuYaol2A&libraries=places&callback=initAutocomplete"></script>
<script>
jQuery(function($) {
// Asynchronously Load the map API
var script = document.createElement('script');
script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
document.body.appendChild(script);
});
function initialize() {
@foreach($flyer as $flyers)
var lat = {{ $flyers->lat }};
var lng = {{ $flyers->lng }};
@endforeach
var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: 'roadmap'
};
// Display a map on the page
map = new google.maps.Map(document.getElementById("map"), mapOptions);
map.setTilt(45);
// Multiple Markers
var markers = [
[lat, lng ]
];
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(), marker, i;
// Loop through our array of markers & place each one on the map
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]
});
// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.open(map, marker);
}
})(marker, i));
// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds);
}
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
this.setZoom(14);
google.maps.event.removeListener(boundsListener);
});
}
</script>
As you can see im looping through each flyer. When I go to see the webpage source, it displays through all the LAT and LNG in that foreach, but right here:
正如你所看到我循环浏览每个传单。当我去看网页源时,它会显示该foreach中的所有LAT和LNG,但就在这里:
// Multiple Markers
var markers = [
['', lat, lng ]
];
it only displays the last lng and lat, do u guys know, maybe I got to insert it into an array or something, I tried many things? That's what it looks like right now with 1 marker:
它只显示最后的lng和lat,你们知道吗,也许我要将它插入数组或其他东西,我尝试了很多东西?这就是现在用1个标记看起来的样子:
1 个解决方案
#1
0
Got it! This is what I had to do:
得到它了!这就是我必须做的事情:
// Multiple Markers
var markers = [
@foreach($flyer as $flyers)
['', {{ $flyers->lat }}, {{ $flyers->lng }} ],
@endforeach
];
#1
0
Got it! This is what I had to do:
得到它了!这就是我必须做的事情:
// Multiple Markers
var markers = [
@foreach($flyer as $flyers)
['', {{ $flyers->lat }}, {{ $flyers->lng }} ],
@endforeach
];