I've created a Google Map and have drawn a polyline on it. I've then added a marker to the start of the polyine (same coords as the starting coords of the polyline).
What I'd like to be able to do, is grab and drag the marker but have it "stick" to the polyline such that you can only drag it along the polyline and not away or to the side of it.
Is it possible to confine a draggable marker to a path in GM V3? If not, can anyone think how this might be done? There's the possibility of snapping the marker to the nearest point on the path when the user drops it, but I'd prefer a smoother "drag along the path" effect.
Happy to have ArcGis suggestions too. Have not provided code as this is more of an in theory question.
Let me know if I need to explain further.
Thanks in advance
Thank you for this solution.
To get it working without dependencies, I had to modify a couple of lines:
First, check the toRad() function exists:
And also, remove _. dependency by replacing the return code:
and finally, include distances[] before distancekeys[]
Ok so I have managed to solve this. It's not exactly elegant, and I'm sure it could be improved on but here's the general concept I've come up with:
I create an array of latlng points from a GPX file, but they only log points every 20s or so. That's not enough granularity for my purposes so what I did is I padded the array of points with about 10 points (on a linear line) between each pair of points logged by the gpx:
Now that I have a more refined array of points, I use this to plot a polyline. The padded polyline will look no different to a polyline drawn without the padding, as all of the additional points lie on a linear "as-the-crow-flies" line between the existing points.
Now I add a draggable marker at the start of the line:
All that's left to do is control the drag and dragend events of this marker:
Here we simply send the latLng of the marker to a function find_closest_point_on_path() whilst dragging and when the marker is dropped. We send the padded array of points as a path to search.
The function looks like this:
What this function does (with the help of a degrees to radians function) is it finds the distance between the markers position and all of the points on the line. Then it finds the closest point to the marker and returns the coordinates for the very next closest point after that one. This way, when you drag or drop the marker it "snaps" to the next point (rather than getting stuck in one spot).
Working JS Fiddle Below:
http://jsfiddle.net/Z5GwW/4/
Have not cross-browser tested. Working in Chrome latest version.