Google Maps panTo conflicts with addListener
Google Maps panTo conflicts with addListener
我有一个函数,可以在单击时将地图重新居中标记。
google.maps.event.addListener(marker, 'click', function() {
map.panTo(marker.getPosition());
});
但是,这显然会影响我在获取标记时拥有的 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]);
}
}
});
有没有办法对addListener进行例外,或者另一种简单的解决方法?谢谢!
一种快速解决方法是在平移地图之前移除事件侦听器,然后在地图完成平移后再次添加事件侦听器。
// 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]);
}
}
});
}
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- How to declare a Map containing certain properties with flow
- 设置'这'在React with Inverse Data Flow中
- Unit-testing multiple use cases with Karma & Mocha.js
- Modify Javascript with C#
- 谷歌地图API v3:Initial View is Fine,but Gray Box with No Map if
- Google Maps panTo conflicts with addListener
- chrome.browserAction.onClicked.addListener() with popup