我想跟踪 map 中心的坐标。到目前为止,我一直在使用它:
// On Drag End
google.maps.event.addListener(map, 'dragend', function() {
$('.map_center_coords .latitude').html( map.getCenter().lat() );
$('.map_center_coords .longitude').html( map.getCenter().lng() );
});
通过在拖动事件结束时获得坐标来工作。
问题是我现在正在使用
map.panTo
移到某个位置。是否有基本上是
"whenever the center has *finished* moving in any way"
的事件?如geocodezip所述,我忘记了
center_changed
事件。但是在拖动/平移 map 时会连续触发该事件。理想情况下,在任何拖动/平移完成后,我都希望查找仅触发一次的事件。
最佳答案
相反,请观察idle-event(在平移或缩放后 map 变为idle时将触发此事件):
google.maps.event.addListener(map,'idle',function(){
if(!this.get('dragging') && this.get('oldCenter') && this.get('oldCenter')!==this.getCenter()) {
//do what you want to
}
if(!this.get('dragging')){
this.set('oldCenter',this.getCenter())
}
});
google.maps.event.addListener(map,'dragstart',function(){
this.set('dragging',true);
});
google.maps.event.addListener(map,'dragend',function(){
this.set('dragging',false);
google.maps.event.trigger(this,'idle',{});
});
关于google-maps-api-3 - 拖动和平移完成后获取中心坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25539734/