[英]Google Maps panTo conflicts with addListener
I have a function that recenters the map around the marker when clicked.我有一个功能,可以在单击时围绕标记重新调整地图。
google.maps.event.addListener(marker, 'click', function() {
map.panTo(marker.getPosition());
});
However, this obviously affects the addListener I have when getting the markers:但是,这显然会影响我在获取标记时的 addListener:
google.maps.event.addListener(map, 'idle', function() {
$.get("/map.json", function(galleries) {
var bounds = map.getBounds();
for (var i = 0; i < galleries.length; i++) {
var latitude = galleries[i].latitude;
var longitude = galleries[i].longitude;
var position = new google.maps.LatLng(latitude, longitude)
if (bounds.contains(position)){
bindInfobox(map, galleries[i]);
}
}
});
Is there a way to make an exception to the addListener, or another easy way around this?有没有办法对 addListener 进行例外处理,或者另一种简单的方法来解决这个问题? Thanks!
谢谢!
A quick workaround can be remove the event listener before you pan your map and then add the event listener again once the map has finished panning.一个快速的解决方法是在平移地图之前删除事件侦听器,然后在地图完成平移后再次添加事件侦听器。
// add map idle event at load time
google.maps.event.addListener(map, 'idle', mapIdle);
//remove map idle event and then re-attach the event
google.maps.event.addListener(marker, 'click', function () {
google.maps.event.clearListeners(map, 'idle');
google.maps.event.addListenerOnce(map, 'idle', function () {
//add the idle event once the map has finised panning
google.maps.event.addListener(map, 'idle', mapIdle);
});
map.panTo(marker.getPosition());
});
function mapIdle() {
$.get("/map.json", function (galleries) {
var bounds = map.getBounds();
for (var i = 0; i < galleries.length; i++) {
var latitude = galleries[i].latitude;
var longitude = galleries[i].longitude;
var position = new google.maps.LatLng(latitude, longitude)
if (bounds.contains(position)) {
bindInfobox(map, galleries[i]);
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.