我使用的是谷歌地图应用程序接口v3。我需要捕获地图上的所有idle事件,但我不希望在通过代码更改边界或缩放时看到它们。当我使用fitBounds方法时,我的问题存在。我使用一个标志来决定一个事件是因为我还是用户而创建的。当我使用fitBounds时,如果边界改变了,它就会被触发,但有时边界不会改变,事件也不会被触发,所以标志是错误的。它会导致脚本忽略用户的下一个事件。下面是我的代码:
var flag = false;
google.maps.event.addListener(map, 'idle', function() {
if(flag) {
// ignore this event
} else {
// do some work
}
flag = false;
});
function fitBounds() {
var bounds = new google.maps.LatLngBounds();
for(var i=0; i<markers.length; i++) {
var location = markers[i].getPosition();
bounds.extend(location);
}
// I need something like whether bounds will change or not after fitBounds?
flag = true;
map.fitBounds(bounds);
}发布于 2012-08-13 16:14:23
您在错误的位置将标志重置为false。试着这样做:
google.maps.event.addListener(map, 'idle', function() {
if(flag) {
// ignore this event
flag = false; // reset to false here
return;
}
else {
// do some work
}
});
function fitBounds() {
flag = true;
// rest of the function....
}除此之外,最好将侦听器附加到bounds_changed,而不是idle事件。
发布于 2012-08-13 15:51:05
我不确定它是否会像api在计算边界时考虑控件(在地图的边上),有时不考虑,但我认为值得一试:
if (!map.getBounds().contains(bounds.getNorthEast()) ||
!map.getBounds().contains(bounds.getSouthWest()))
{
flag = true;
map.fitBounds(bounds);
}https://stackoverflow.com/questions/11929280
复制相似问题