google-maps - 如何在 DirectionsRenderer 上获取拖动点

标签 google-maps

我使用谷歌地图绘制我的路线。我只设置了 2 个点(开始和结束)。问题是我必须将路由发送到下一页。

我知道我可以捕获事件:

        google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
            $("#full-route-json").val(encode64(JSON.stringify(directionsDisplay.directions.routes[0])));
        });

但它会返回所有步骤。 如何仅获取我的起点和终点以及拖动点的数据,如下所示: enter image description here

最佳答案

据我所知,没有实现的方法来访问这些点(标记)。

但是这些标记存储在 DirectionsRenderer 实例的属性中。

以下代码将为您提供这些详细信息(请注意,此代码现在可以运行,但当 API 更改时可能会失败)

  google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
    var that=this;
    setTimeout(function(){//we need a short delay
      for(var k in that){//iterate over all properties
        if(typeof that[k].markers!='undefined'){//find the desired property
          var markers=that[k].markers,arr=[];
          for(var i=0;i<markers.length;++i){//collect the data
            arr.push(markers[i].position.toString());
        }
        alert(arr.join(',\n'));
      }
    }},100)
  });

关于google-maps - 如何在 DirectionsRenderer 上获取拖动点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18664480/

相关文章:

android - Android 上的谷歌地图滞后

javascript - 将动态 CSS 样式应用于谷歌地图的标记

google-maps - 动态添加标记以 flutter 谷歌地图

google-maps - 使用 Google Maps API 重新创建嵌入的标准 Google Map

javascript - 在谷歌地图 V3 上绘制阴影区域 - Javascript

javascript - 如何在谷歌地图中隐藏河流和湖泊?

ios - 无法使用 Google Map SDK 为 GMSPolyline 设置颜色

javascript - 谷歌地图 : Cannot read property 'geometry' of undefined

javascript - 使用 Google Maps Javascript API v3 显示楼层按钮控件

google-maps - 即使地理编码街道不存在,Google Maps API v3 地理编码状态也正常