I have a google map that updates all the markers on it upon dragend
and zoom_changed
events. It's all working except the initial load of the map. After the map has loaded it is empty. At the end of initialize()
I call google.maps.event.trigger(map, 'dragend')
to try to call the dealWithNewWindow
function so the map fills with markers, but nothing happens?
initialize = function() {
mapOptions = {
center: { lat: 51, lng: 0 },
zoom: 9
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
map.setCenter(new google.maps.LatLng(51,0));
google.maps.event.addListener(map, 'zoom_changed', dealWithNewWindow);
google.maps.event.addListener(map, 'dragend', dealWithNewWindow);
function dealWithNewWindow(event) {
killAllMarkers();
var bounds = map.getBounds();
var nelat = bounds.getNorthEast().lat();
var swlat = bounds.getSouthWest().lat();
var nelng = bounds.getNorthEast().lng();
var swlng = bounds.getSouthWest().lng();
var mapBounds = {NElatitude:nelat, SWlatitude:swlat, NElongitude:nelng, SWlongitude:swlng};
$.ajax({
type : 'POST',
url : '/maprequest',
dataType : 'script',
data : { NElatitude:nelat, SWlatitude:swlat, NElongitude:nelng, SWlongitude:swlng }
});
};
google.maps.event.trigger(map, 'dragend');
};
google.maps.event.addDomListener(window, 'load', initialize);
EDIT:
I've homed in a bit on the problem. After a page refresh the javascript grinds to a halt during this line in dealWithNewWindow: var nelat = bounds.getNorthEast().lat();
. For some reason this code runs fine when the map bounds are changed afterwards, but it fails the first time the map is loaded.