I'm rendering a polyline on a Google map with data that looks like this:
data = [
{
distance: 3.45,
lat: 37.37112776376307,
lng: -122.13909476995468,
},
...
];
When hovering over the polyline, I can get the lat
/lng
coordinates and I want to get the distance along the line based on those coordinates.
I haven't found an easy way to do this with the Maps API. I want the distance from the start to an arbitrary point along the polyline; most answers I've found deal simply with the (shortest) distance between two points, not along a predefined route.
My other thought was to use D3 (which I'm using to draw some related charts) to bisect the data and get the distance. However, I can't figure out how to bisect based on two data points (lat
+ lng
).
Any advice on how to accomplish what I'm trying to do?
EDIT: In my data, distance
is the cumulative distance traveled at the given coordinates.