Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating markers on Google Maps v2

What is the best way to animate markers on Google Maps using v2 API?

I am working on a map-centered game where I track locations of people and display them on the map for each other to see. As people move, I want to animate a marker from his current to his latest position. Every person has a direction, so I need to rotate the marker appropriately.

What is the best way to do it using the new Google Maps API?

like image 325
Maksim Golivkin Avatar asked Feb 13 '13 22:02

Maksim Golivkin


People also ask

How do I animate a marker on Google Maps?

If you want to animate marker back-and-forth, just toggle setPosition each second. setTimeout(function() { var newPosition = /* select new position */ marker. setPosition(newPosition) }, 1000);


1 Answers

Some Google engineers have provided a nice demo video with some elegant sample code about how to animate markers from a starting point to an ending point, for all various versions of Android:

The relevant code is here:

https://gist.github.com/broady/6314689

And a nice demo video of all of it in action.

http://youtu.be/WKfZsCKSXVQ

OLD DEPRECATED ANSWER BELOW

In the documentation, it is mentioned that Marker Icons cannot be changed:

Icon

A bitmap that's displayed for the marker. If the icon is left unset, a default icon is displayed. You can specify an alternative coloring of the default icon using defaultMarker(float). You can't change the icon once you've created the marker.

Google Maps API v2 Documentation

You're going to have to keep track of specific markers, perhaps using a method similar to that described here: Link a Marker to an Object, then figure out which marker you need to update. Call .remove() on the marker, then create a rotated image depending on the "direction" you want, create a new Marker with that image, and add the new Marker to the map.

You do not need to "clear" the map, simply remove the marker you want to modify, create a new one, then add it back to the map.

Unfortunately, the new Maps API is not very flexible yet. Hopefully Google continues to improve upon it.

like image 179
DiscDev Avatar answered Sep 28 '22 19:09

DiscDev