Skip to content Skip to sidebar Skip to footer

Google Maps Api V3 Not Loading Without Fitbounds, Zooming Causing Infinite Loop/stackoverflow

Previous question here: stack overflow with Google maps API (IE7 IE8) I found the following question in the mean time: Google Maps API v3: Can I setZoom after fitBounds? The soluti

Solution 1:

What I was missing from the answer from my previous question was that an initial zoom level and center has to be set.

varmyOptions= {
            mapTypeId:google.maps.MapTypeId.ROADMAP,
            mapTypeControl:false,
            zoom:22,
            center:newgoogle.maps.LatLng(50.820645,-0.137376)
    };

Also had to change the event to 'idle' on the multi marker zooming adjustment.

if (markers.length>1){
    var markerCluster = new MarkerClusterer(map, markers, mcOptions);
    map.fitBounds(bounds);
    google.maps.event.addListenerOnce(map, 'idle', function() {
        var oldZoom = map.getZoom();
        map.setZoom(oldZoom + (-7)); //Or whatever
    });
}

Works like a charm after that.

Solution 2:

When you have only one hotel, you create a LatLngBounds object containing only one point:

for (i = 0; i < hoteldata.length; i++) {
    var latLng = new google.maps.LatLng(hoteldata[i][1], hoteldata[i][2]);
    bounds.extend(latLng);
...}

but you then do a map.fitBounds(bounds) — this attempts an infinite zoom. While it's arguable that the API should be able to cope with that, it's equally arguable that it will attempt to do exactly what you tell it. It's possible that IE will behave differently to Firefox and other browsers.

Where you have commented out the fitBounds() in your quoted code in the question, that is present in your online page. Since that line only applies to instances where one marker is involved and the bounds object is a single point, I would replace it with a simple setZoom() instead.

map.setZoom(16);

Post a Comment for "Google Maps Api V3 Not Loading Without Fitbounds, Zooming Causing Infinite Loop/stackoverflow"