Animating a marker on a section of a polyline

1,371 views
Skip to first unread message

Rob

unread,
Sep 4, 2011, 5:54:17 PM9/4/11
to Google Maps JavaScript API v3
I have a polyline from A to B and I'm trying to animate a marker
moving along a section of the line (between two points - essentially I
want to show a vehicle moving along part of a route). Using v3_epoly
I've been able to animate from start to finish, but not a subset of
the route.

What I thought I could do is find the distance along the route of the
first marker/latlng, then do the same with the second and animate
between the two distances, but I can't find a way of getting the
distance along the polyline of a given latlng.

Can anyone give any pointers?

Thanks in advance!
Message has been deleted

Chris Broadfoot

unread,
Sep 4, 2011, 9:22:50 PM9/4/11
to google-map...@googlegroups.com
Here's some code I hacked up a while ago:

Chris


--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


Rossko

unread,
Sep 4, 2011, 9:28:13 PM9/4/11
to Google Maps JavaScript API v3
> What I thought I could do is find the distance along the route of the
> first marker/latlng, then do the same with the second and animate
> between the two distances, but I can't find a way of getting the
> distance along the polyline of a given latlng.

You might use the GetPointAtDistance() method of v3_epoly

geoco...@gmail.com

unread,
Sep 4, 2011, 9:46:16 PM9/4/11
to Google Maps JavaScript API v3
Perhaps if you give a link to your map where you tried to do that, we
could see where you went wrong.

If you are using v3_epoly to animate it the whole length, you should
be able to use v3_epoly to find your start and end points.

-- Larry

Rob

unread,
Sep 5, 2011, 6:06:47 PM9/5/11
to Google Maps JavaScript API v3
I was hoping to find a more efficient approach but what you suggested
did the trick. I traverse the polyline once from start to finish, at
each stage checking whether the current lat/lng is also a marker's
location - if it is then I store the distance as part of the marker
object. Then the animation is just a case of several more
GetPointAtDistance() calls, with the value of distance bounded by the
two markers.

Thank you for your help.
Reply all
Reply to author
Forward
0 new messages