How to drag point to DirectionsRenderer

I use google maps to draw my route. I set up only 2 points (start and end). The problem is that I have to send the route to the next page.

I know I can catch an event:

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

But he will return all the steps. How to get data only for my start and end points and drag points as follows: enter image description here

+3
source share
1 answer

AFAIK there is no implemented method of access to these points (markers).

But these tokens are stored inside the property of the DirectionsRenderer instance.

The following code will provide you with these details (note that this code is working now, but may crash when changing the 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) }); 
+1
source

Source: https://habr.com/ru/post/953720/


All Articles