拖放后获取中心坐标锅完成 [英] Get center coords after drag & pan finished
问题描述
我想跟踪地图中心的坐标。到目前为止,我一直在使用这个:
//在拖动结束
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
移动到某个位置。
基本上是的事件,只要中心有*完成*以任何方式移动?
<正如geocodezip所提到的,我忘记了
center_changed
事件。但是,当地图被拖动/平移时,该事件不断被触发。 理想情况下,我正在寻找一个事件,只有在拖动/平移后才会触发一次
观察空闲事件代替(当地图在平移或缩放后变为空闲时,会触发此事件):
google.maps.event.addListener(map,'idle',function(){$ b $ this if('dragging')&&& this.get('oldCenter')&& this.get('oldCenter')!== this this.getCenter()){$ b $如果(!this.get('dragging')){
this.set('oldCenter',this.getCenter())$ b {//做你想做的事
}
$ b}
});
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',{});
});
I would like to keep track of the coordinates of the center of my map. So far I've been using this:
// 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() );
});
which works by getting the coords at the end of the drag event.
The problem is I'm now using map.panTo
to move to a certain location.
Is there an event for basically "whenever the center has *finished* moving in any way"
?
As mentioned by geocodezip, I forgot about the center_changed
event. But that event is continuously fired while the map is being dragged/panned.
Ideally I'm looking for an event that is only fired once, after any dragging/panning is done.
Observe the idle-event instead(This event is fired when the map becomes idle after panning or zooming):
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',{});
});
这篇关于拖放后获取中心坐标锅完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!