我需要在地图上的主页上显示多个标记。我;在数据库中存储每个旅行传单和一个lng和lat,然后我为每个传单显示它。 (哪个工作正常),但是当我使用此代码在我的主页上显示它时,它只显示插入数据库的最后一个lat和lng。
这是我的代码:
<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>
正如你所看到我循环浏览每个传单。当我去看网页源时,它显示在该foreach中的所有LAT和LNG,但就在这里:
// Multiple Markers
var markers = [
['', lat, lng ]
];
它只显示最后的lng和lat,你知道吗,也许我要将它插入数组或其他东西,我尝试了很多东西? 这就是现在用1个标记看起来的样子:
答案 0 :(得分:0)
知道了!这就是我必须要做的事情:
// Multiple Markers
var markers = [
@foreach($flyer as $flyers)
['', {{ $flyers->lat }}, {{ $flyers->lng }} ],
@endforeach
];