简体   繁体   English

缩放更改时,google maps v3 zoom_changed事件不会触发

[英]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. 我有一个v3谷歌地图正在按照我的预期加载,标记符合我的意图。 However when I change the zoom, the zoom_changed event that I have added does not appear to fire. 但是,当我更改缩放时,我添加的zoom_changed事件似乎不会触发。 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... 2件事......

  1. Right now your zoom_changed listener isn't being added becuase it's called before the map is initialized. 现在你没有添加你的zoom_changed监听器,因为它是在初始化地图之前调用的。 Javascript executes the map_initialise() function then immediately tries and add the listener before the map is finished loading. Javascript执行map_initialise()函数,然后在地图加载完成之前立即尝试并添加侦听器。 So put the addListener into the initialize function at the end. 所以将addListener放在最后的initialize函数中。

  2. Your map variable is private to the map_initialise() function so when reCentre() is called it can't see your map object. 您的map变量对map_initialise()函数是私有的,因此当调用reCentre()时,它无法看到您的地图对象。 If you remove var from in front of map it will become global and reCentre() will be able to see it. 如果从地图前面删除var ,它将变为全局变量,并且reCentre()将能够看到它。 I recommend adding var map; 我建议添加var map; above the map_initialise() function so readers of the code will see map is global. 在map_initialise()函数之上,所以代码的读者会看到map是全局的。

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; zoom_changed的监听器确实被添加,并在那一刻触发一次; 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: 但此后,每次单击缩放控件都会导致以下消息显示在Firefox的错误控制台中:

Error: ge is undefined 错误:ge未定义
Source File: http://maps.gstatic.com/intl/en_us/mapfiles/api-3/10/20/main.js 源文件: http//maps.gstatic.com/intl/en_us/mapfiles/api-3/10/20/main.js
Line: 19 行: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: ge is undefined 错误:ge未定义
Source File: http://maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/10/20/%7Bmain,places%7D.js 源文件: http//maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/10/20/%7Bmain,places%7D.js
Line: 19 行: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侦听器之前添加标记和其他侦听器(用于在地图上和标记上“单击”),并且它们始终可靠地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM