Struggling with the Google Maps API v3 to drop markers one at a time on my map? I want it to work like the Google Demo, but all markers are dropping at the same time. Here's my code:
var map;
var markers = [];
function initialize() {
var latlng = new google.maps.LatLng(52.520816, 13.410186);
var options = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), options);
}
initialize();
function loadMarkers() {
$.ajax({
url: 'js/poi.php',
type: 'GET',
dataType : "json",
success: function (data) {
var latlngbounds = new google.maps.LatLngBounds();
// Loop through data and drop a marker for each point
$.each(data, function(index, point) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(point.Lat, point.Lng),
animation: google.maps.Animation.DROP,
icon: 'img/marker.png'
});
markers.push(marker);
latlngbounds.extend(marker.position);
});
// Cluster and fit bounds
var markerCluster = new MarkerClusterer(map, markers);
map.fitBounds(latlngbounds);
}
});
}
loadMarkers();
I attempted to use a Timeout on each marker, but it seems that loadMarkers(); is causing them to drop simultaneously.
setTimeout(function() {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(point.Lat, point.Lng),
animation: google.maps.Animation.DROP,
icon: 'img/marker.png'
});
}, point.Id * 200);
Any suggestions on how to resolve this issue?
EDIT: The poi.php file lists all points from my Table and outputs them in the following format:
[
{"Id":"1","Lat":"52.511467","Lgn":"13.447179"},
{"Id":"2","Lat":"52.549061","Lgn":"13.422975"},
{"Id":"3","Lat":"52.497622","Lgn":"13.396110"},
{"Id":"4","Lat":"52.517683","Lgn":"13.394393"}
]