如何获得DirectionsRenderer上的拖拽点 [英] How to get dragged point on the DirectionsRenderer
问题描述
我使用谷歌地图来绘制我的路线。我只设置了2个点(开始和结束)。问题是,我将不得不将路线发送到下一页。
我知道我可以捕捉到事件:
google.maps.event.addListener(directionsDisplay,'directions_changed',function(){
$(#full-route-json)。val(encode64 (JSON.stringify(directionsDisplay.directions.routes [0])));
});
但它会返回所有步骤。
如何仅获取我的起点和终点的数据,并像这样拖动点:$ b $ b
AFAIK没有实现方法来访问这些点(标记)。
但是这些标记存储在DirectionsRenderer实例的属性中。 下面的代码会给你这些详细信息(请注意,此代码现在可以使用,但API更改时可能会失败)
google.maps.event。 addListener(directionsDisplay,'directions_changed',function(){
var that = this;
setTimeout(function(){//我们需要一个简短的延迟
for(var k in that){ //遍历所有的属性
if(typeof that [k] .markers!='undefined'){//找到想要的属性
var markers = that [k] .markers,arr = [] ;
for(var i = 0; i< markers.length; ++ i){//收集数据
arr.push(markers [i] .position.toString());
alert(arr.join(',\ n'));
}
}},100)
});
I use google maps for drawing my route. I setup only 2 points (start and end). The problem is that I will have to send the route to the next page.
I know that I can catch event:
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
$("#full-route-json").val(encode64(JSON.stringify(directionsDisplay.directions.routes[0])));
});
But it will return all steps. How to get the data only for my start and end points and dragged point like this:
AFAIK there is no implemented method to access these points(markers).
But these markers are stored inside a property of the DirectionsRenderer-instance.
The following code will give you these detail(please note that this code works now, but it may fail when the API changes)
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)
});
这篇关于如何获得DirectionsRenderer上的拖拽点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!