我使用谷歌地图绘制我的路线。我只设置了 2 个点(开始和结束)。问题是我必须将路由发送到下一页。
我知道我可以捕获事件:
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
$("#full-route-json").val(encode64(JSON.stringify(directionsDisplay.directions.routes[0])));
});
但它会返回所有步骤。 如何仅获取我的起点和终点以及拖动点的数据,如下所示:
最佳答案
据我所知,没有实现的方法来访问这些点(标记)。
但是这些标记存储在 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/