[英]google maps api v3 zoom_changed event fired twice after fitBounds called
I'm working on a web app that integrates with google maps and I'm having an issue with zoom_changed event firing twice after calling fitBounds. 我正在开发一个与谷歌地图集成的网络应用程序,我在调用fitBounds后遇到zoom_changed事件触发两次的问题。 The only reference I have to fitBounds is in updateMap.
我必须使用fitBounds的唯一引用是在updateMap中。 The only time I attach the event listener is in createMap.
我附加事件监听器的唯一时间是在createMap中。
The second call to the zoom changed handler is causing another trigger to update the map. 对zoom更改处理程序的第二次调用导致另一个触发器更新映射。
I've read this article already: Do not fire 'zoom_changed' event when calling fitBounds function on map 我已经读过这篇文章: 在地图上调用fitBounds函数时不要触发'zoom_changed'事件
The flag is working fine for the first event. 该标志适用于第一个事件。 But why is there another zoom_changed event being fired?
但为什么还有另一个zoom_changed事件被触发?
Even better, how can I prevent the second zoom_changed event from firing? 更好的是, 如何防止第二个zoom_changed事件被触发?
Here is what I'm seeing in my console log: 这是我在控制台日志中看到的内容:
updateMap, fitbounds: 0
calling fitbounds...
zoom changed
mapEventHandler, fitbounds: 1
zoom changed
mapEventHandler, fitbounds: 0
Here is the code for my update map function which calls fitBounds: 这是我的更新映射函数的代码,它调用fitBounds:
var mapZoomOrDragEventInProgress = false;
var fitBoundsCalledCount = 0;
var map = null;
updateMap = function () {
console.log("updateMap, fitbounds: " + fitBoundsCalledCount);
var bounds = fitBoundsForVenues();
deleteAndUpdateMarkers();
if (!mapZoomOrDragEventInProgress) {
bn.spg.map.vars.fitBoundsCalledCount++;
if (markers.length > 1) {
console.log("calling fitbounds...");
map.fitBounds(bounds);
} else {
console.log("setting zoom...");
map.setCenter(bounds.getCenter());
map.setZoom(13);
}
}
mapZoomOrDragEventInProgress = false;
}
Here is my create map function: 这是我的创建地图功能:
createMap = function() {
if (map === null) {
var bounds = fitBoundsForVenues();
var mapOptions = {
center: bounds.getCenter(),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
google.maps.event.addListener(map, 'zoom_changed', zoomChangedEventHandler)
google.maps.event.addListener(map, 'dragend', dragendEventHandler);
}
}
Here is my event handler: 这是我的事件处理程序:
zoomChangedEventHandler = function () {
console.log("zoom changed");
console.log("mapEventHandler, fitbounds: " + fitBoundsCalledCount);
//we want to handle only user zoom events, NOT zoom_events triggered from fit bounds or set_zoom
if (fitBoundsCalledCount === 0) {
mapZoomOrDragEventInProgress = true;
var coords = getViewportCoordinates();
updateVenuesAndMapAsync(coords.lat, coords.lng, coords.radius);
} else {
fitBoundsCalledCount--;
}
}
I can't tell you where the 2nd zoom_changed
-even has been triggered(I'm sure a single call of fitBounds()
is not be the reason) 我无法告诉你第二个
zoom_changed
被触发的位置(我确定只有一次调用fitBounds()
不是原因)
But instead of using these counters I would suggest to remove the zoom_ changed
-listener at the begin of updateMap()
and to re-assign the listener at the end of updateMap()
但是,而不是使用这些计数器我建议删除zoom_
changed
章24 -listener在开始的updateMap()
并在年底重新分配监听updateMap()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.