3

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

我知道我可以捕捉事件:

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

但它会返回所有步骤。如何仅获取我的起点和终点以及拖动点的数据,如下所示: 在此处输入图像描述

4

1 回答 1

2

AFAIK 没有实现的方法来访问这些点(标记)。

但是这些标记存储在 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)
  });
于 2013-09-06T23:00:52.193 回答