Search code examples
androidgoogle-mapsgoogle-maps-android-api-2

Change marker icon during runtime


Is there a way to change Google Maps Android API v2 marker's icon during runtime without removing/re-adding the marker I want to change its icon? Can I apply transformations to it (like rotation)?

Thanks.


Solution

  • Currently you can't change the marker at runtime, neither apply rotation to it.

    You can use a workaround though - I'm working on a BlinkingMarker class where I have to adjust the opacity of the marker image at runtime.

    The only solution right now is to create Bitmaps with different rotation and then add/remove them periodically. The problem with this solution is that adding/removing markers takes a lot of memory allocation so it results in constant garbage collection. A better and smoother workaround is to create all your images up-front and add all of them to the map at once. After that you can use the Marker.setVisible(boolean) function to display the one that you currently need.

    Caution: measure your bitmaps before you do this, because adding a lot of big bitmaps can cause your app's memory size to grow very big.

    You can look at my workaround here: https://github.com/balazsbalazs/blinking-marker-mapsv2

    This is marker which is blinking (changes the opacity of the bitmap) but on the same lines you can apply any kind of transformation.