Skip to content
Advertisement

Position of a dragged latlon Google Map JavaScript

Below code shows the path between two latlongs :

JavaScript

The code always works, I can see the path all the time when I drag those two latlongs. But I want to get the position of latlongs after dragging. How can I do that? I tried to put alerts in various places in the codes but none of them worked. Can you help me with that?

JavaScript
JavaScript
JavaScript

Advertisement

Answer

The markers locations are in the directions property of the directionsDisplay. To retrieve them when the route changes, add an event listener to the directionsDisplay for the directions_changed event, parse the directions object returned for the start and end locations of the route. For your route, with only a single leg, they will be:

JavaScript

To put them in a <input> field on the page:

JavaScript

proof of concept fiddle

code snippet:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement