开发者

google maps v3 zoom_changed event does not fire when zoom changes

I have a v3 Google map being loaded exactly as I expect and the marker does what 开发者_开发技巧I intend. However when I change the zoom, the zoom_changed event that I have added does not appear to fire. Would anyone be able to shed any light on why? My code is below.

function map_initialise() {
    var mapCentre = new google.maps.LatLng(53.75, -1.50);
    var mapOptions = {
        zoom: 6,
        center: mapCentre,
        mapTypeId: google.maps.MapTypeId.HYBRID
    }

    var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

    var latlong1 = new google.maps.LatLng(52.456550,1.680182);
    var windowtext1 = 'Some text';
    var popup1 = new google.maps.InfoWindow({
        content: windowtext1
    });
    var marker1 = new google.maps.Marker({
        position: latlong1,
        title: "Some text"
    });
    google.maps.event.addListener(marker1, 'click', function() { 
        popup1.open(map,marker1);
    });
    marker1.setMap(map);
}

google.maps.event.addDomListener(window, 'load', map_initialise);

google.maps.event.addListener(map, 'zoom_changed', function() {
    setTimeout(reCentre, 3000);
});

function reCentre() {
    var newcentre = new google.maps.LatLng(53.000,0.000);
    map.panTo(newcentre);
}


2 things...

  1. Right now your zoom_changed listener isn't being added becuase it's called before the map is initialized. Javascript executes the map_initialise() function then immediately tries and add the listener before the map is finished loading. So put the addListener into the initialize function at the end.

  2. Your map variable is private to the map_initialise() function so when reCentre() is called it can't see your map object. If you remove var from in front of map it will become global and reCentre() will be able to see it. I recommend adding var map; above the map_initialise() function so readers of the code will see map is global.


If the recommended solution worked, it may have been just a coincidence related to a small, simple map. On a large hybrid map, it doesn't solve the problem, which is actually more complicated. The listener for zoom_changed does get added, and fires once at that moment; the action part of that listener executes correctly then. But thereafter, every click on the zoom control causes the following messages to appear in Firefox's error console:

Error: g.e is undefined
Source File: http://maps.gstatic.com/intl/en_us/mapfiles/api-3/10/20/main.js
Line: 19

and the listener action is not executed.

If the "places" library is included (to support a search box), the behavior is the same but the source file in the error message is different:

Error: g.e is undefined
Source File: http://maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/10/20/%7Bmain,places%7D.js
Line: 19

What makes the suggested solution even more suspicious is that markers and other listeners (for "click" on the map and on the markers) can be added before the zoom_changed listener and they always work reliably.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜