0

I have a Google map like so:

var map_options = {        
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    streetViewControl: false
};
var map = new google.maps.Map(mapDiv, map_options);

I perform an ajax call that puts some markers on the map and in that call's beforeSend I change the zoom level of the map based on the place the user selects. This is all fine and dandy..

$.ajax({
    url: myServiceUrl,
    dataType: 'jsonp',
    beforeSend: function () {                    
        setMapZoom();
    },            
    success: function (data) {
        //do work
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(errorThrown);
    }            
});

function setMapZoom() {    
    var z_level = getMapZoomLevel(googleplace.geometry.viewport);
    map.setCenter(googleplace.geometry.location);
    map.setZoom(z_level);
}

At first this wasn't working at all because the map api's calls are async.. so that's why I put the zoom method in the beforeSend.

However, NOW my issue is that it only performs the zoom ONE TIME. I load the map on say Toronto, ON, Canada - it goes and centers on Toronto and zooms to level 5 - perfect.

Then I change to Miami, FL, USA - it centers on Miami just fine, but it determines the zoom to be level 9 (not 5) calls map.setZoom(9) - but the zoom stays at 5.

I appreciate any feedback. :)

Thanks!

5
  • I think your code do setZoom(9) during the map still loading tiles.
    – wf9a5m75
    Nov 13, 2012 at 19:02
  • The setMapZoom() function is called before the markers are even requested though.. Nov 13, 2012 at 19:12
  • I see. Could you give me a link of your page?
    – wf9a5m75
    Nov 13, 2012 at 19:14
  • I actually can't due to a Non Disclosure Agreement. Is there any other information I might be able to provide that could help you? I could list the work flow of the page in detail if that helps. Nov 13, 2012 at 19:50
  • jsbin.com/welcome/48301 - it works as I want it to :/ So I believe that wf is correct when he says the markers are loading during the setZoom.. any thoughts on how to fix this? Nov 13, 2012 at 21:07

2 Answers 2

0

You have the viewport desired, why don't you use it?

map.fitBounds(googleplace.geometry.viewport);
1
  • I have tried this. I believe now after setting this up in jsbin that the first commenter is correct. The loading of markers is taking too long. Nov 13, 2012 at 21:05
0

SOLVED:

As it turns out.. a fellow coder here was calling some code to clear user defined listeners on the zoom event. He didn't know this also clear Google defined listeners.. :)

Maybe this will help someone in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.