google-maps-api-3 - 拖动和平移完成后获取中心坐标

标签 google-maps-api-3

我想跟踪 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/

相关文章:

jquery - Google 按类名自动完成位置不起作用

javascript - 单击侧边栏元素上的 Google Cluster map 打开特定信息窗口

php - 使用 Google Map API v3 提供的 DirectionsService 查找并显示最近标记的路线

javascript - 当用户点击时更改 Google map 标记图标

google-maps - Google Maps Javascript API v3 随机发生 NotLoadingAPIFromGoogleMapsError

google-maps - 谷歌地图 v3 : geometry library not loaded

android - Google Geocoding API 限制

javascript - 折线段的不同颜色

php - Google Maps API V3 KML 图层不显示

javascript - 如何更改谷歌地图标记颜色