我知道,如果边界发生了更改,就会触发一个bounds_changed事件,但是我需要知道fitBounds何时完成(何时边界已更改或边界保持不变)。
谷歌有一种宽容,如果边界变化很小,它实际上不会改变界限/调用bounds_changed。我需要知道这是什么时候发生的。
我已经成功地完成了超时操作,但是当bounds_changed花费超过100 it的时间触发时,它似乎很麻烦,而且可能容易出错。
参见示例:http://jsbin.com/sosurexuke/1/edit?js,console,output
有更好的解决办法吗?
编辑:如果您只考虑检查map.getBounds()是否与您发送的fitBounds()相同,那么您会失望地知道,符合边界的结果通常并不接近您发送的边界。我写这个函数时认为这是可能的
function boundsAreIdentical(bounds1, bounds2) {
if (!bounds1 || !bounds2) return false;
var tolerance = 0.00001;
console.log(bounds1.getNorthEast().lat(), bounds2.getNorthEast().lat());
console.log(bounds1.getNorthEast().lng(), bounds2.getNorthEast().lng());
console.log(bounds1.getSouthWest().lat(), bounds2.getSouthWest().lat());
console.log(bounds1.getSouthWest().lng(), bounds2.getSouthWest().lng());
if (
(Math.abs(bounds1.getNorthEast().lat() - bounds2.getNorthEast().lat()) <= tolerance) &&
(Math.abs(bounds1.getNorthEast().lng() - bounds2.getNorthEast().lng()) <= tolerance) &&
(Math.abs(bounds1.getSouthWest().lat() - bounds2.getSouthWest().lat()) <= tolerance) &&
(Math.abs(bounds1.getSouthWest().lng() - bounds2.getSouthWest().lng()) <= tolerance)) {
return true;
}
return false;
}2年后编辑:
看起来,在最新的谷歌地图实验版(3.32)中,即使边界与当前边界相同,也会触发bounds_changed。
我将最初的示例更新为3.31版本,该版本仍然显示了原始问题。
发布于 2018-03-18 15:42:27
如对问题的编辑中所示。如果你可以使用3.32,那就用它吧。否则,如果你需要一个解决方案,工作前3.32,那么你可以简单地平移1像素,然后拟合边界。视觉变化不会触发刷新,并强制边界始终更改。
演示:http://jsbin.com/bipekaxosa/1/edit?js,console,output
function pan(map, newCenterLatLng, offsetx, offsety, callback) {
if (!map.getProjection()) {
if (callback) {
return setTimeout(pan, 1, map, newCenterLatLng, offsetx, offsety, callback);
}
throw new Error("You must wait until map.getProjection() is ready. Try using the callback instead");
}
var newCenterLatLngPixels = map.getProjection().fromLatLngToPoint(newCenterLatLng);
var offset = new google.maps.Point(
((typeof (offsetx) == "number" ? offsetx : 0) / Math.pow(2, map.getZoom())) || 0,
((typeof (offsety) == "number" ? offsety : 0) / Math.pow(2, map.getZoom())) || 0
);
map.setCenter(map.getProjection().fromPointToLatLng(new google.maps.Point(
newCenterLatLngPixels.x - offset.x,
newCenterLatLngPixels.y + offset.y
)));
if (callback) {
return callback();
}
}
function fitBoundsAndCallback(map, bounds, callback) {
google.maps.event.addListenerOnce(map, "bounds_changed", function() {
if (callback) {
callback();
}
});
// Pan the map 1 pixel up so that bounds will always change
// even in the event you are fitting to the same bounds as before
// Remove this once the move to gmaps 3.32 as bounds_changed works always
this.pan(map, map.getCenter(), 0, 1, function(){
map.fitBounds(bounds);
});
}发布于 2016-03-03 07:52:00
解决这个问题的第一部分(知道边界何时完成更改)的最简单方法是使用空闲事件侦听器。
无论您如何更改边界,请在即将更改边界之前设置此事件。这样,当边界都确定时,映射将注册为空闲事件,空闲事件会触发,您可以在其中执行shiz操作。为了防止事件堆积在一起,同时清除它。
你可能得摆弄一下。说到摆弄,这里有一把小提琴。在这把小提琴里,我每次都会发出警告:
https://jsbin.com/kapimov/edit?js,output单击“变更边界”,您将看到它一遍又一遍地发生:)
编辑:以检查fitBounds()是否在失败时触发,并在触发fitBounds()之前和之后进行比较。
即使两次尝试将边界设置为相同的坐标,中心似乎也会发生变化,并且可以进行比较。中心检查似乎发生在地图边界的实际移动之前,但在调用该函数时总是会发生变化。
按两次"Fit Current界“按钮就可以看到它在运行;第一次”bounds_changed“点火时,第二次它没有按下,但中心仍然正确检查。注释掉fitBounds()函数,查看中心在没有它的情况下是如何变化的。
https://stackoverflow.com/questions/35761416
复制相似问题