javascript 当缩放更改时,谷歌地图 v3 zoom_changed 事件不会触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4044859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:16:02  来源:igfitidea点击:

google maps v3 zoom_changed event does not fire when zoom changes

javascriptgoogle-maps

提问by Jason

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.

我有一个完全按照我的预期加载的 v3 Google 地图,并且标记符合我的意图。但是,当我更改缩放比例时,我添加的 zoom_changed 事件似乎不会触发。有没有人能够解释为什么?我的代码如下。

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);
}

回答by Galen

2 things...

2件事...

  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 varfrom 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.

  1. 现在您的 zoom_changed 侦听器没有被添加,因为它在地图初始化之前被调用。Javascript 执行 map_initialise() 函数,然后在地图完成加载之前立即尝试并添加侦听器。所以最后把 addListener 放到 initialize 函数中。

  2. 您的地图变量是 map_initialise() 函数的私有变量,因此当 reCentre() 被调用时,它看不到您的地图对象。如果您var从地图前面移除,它将变为全局并且 reCentre() 将能够看到它。我建议var map;在 map_initialise() 函数上方添加,这样代码的读者就会看到 map 是全局的。

回答by user2022975

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:

如果推荐的解决方案奏效,那可能只是与一个小而简单的地图相关的巧合。在大型混合地图上,它并没有解决问题,这实际上更复杂。zoom_changed 的​​侦听器确实被添加了,并且在那一刻触发了一次;该侦听器的动作部分然后正确执行。但此后,每次单击缩放控件都会导致 Firefox 的错误控制台中出现以下消息:

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

错误:ge 未定义
源文件:http: //maps.gstatic.com/intl/en_us/mapfiles/api-3/10/20/main.js
行: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:

如果包含“places”库(以支持搜索框),则行为相同但错误消息中的源文件不同:

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

错误:ge 未定义
源文件:http: //maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/10/20/%7Bmain,places%7D.js
行: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.

使建议的解决方案更加可疑的是,可以在 zoom_changed 侦听器之前添加标记和其他侦听器(用于在地图和标记上“单击”),并且它们始终可靠地工作。