问题
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.
回答1:
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.
回答2:
I am able to change marker icons in runtime easily after updated to Google Play Services Rev 7
, now
Marker.setIcon (BitmapDescriptor icon)
is availble , previously I did remove and add marker to change color of it.
回答3:
The docs are very clear on this issue --
Icon A bitmap that's displayed in place of the default marker image. You can't change the icon once you've created the marker.
If you want to appearance of changing the marker, you have some options. One is, as you note, to delete the marker and add another one. The other is to place multiple markers at the same location and toggle which one is visible at any given time.
Can I apply transformations to it (like rotation)?
You can apply any transformation you like to the image before using it to create the marker.
回答4:
In the September 2013 Google Maps Android API v2 release, there is now a new setRotation() method for markers, along with a new setFlat() method.
Here's Google's description of the new features:
Give your markers a sense of direction
We’ve added a marker rotation property, to allow you to rotate a marker around it’s anchor point. The new flat property allows you make the marker lie flat on the map surface, rather than popping out to face the camera. These two new properties are especially useful for indicating compass directions when the map is rotated or tilted.
Here's an article describing the new features.
回答5:
To change the selected marker icon in the run-time, just add
@Override
public boolean onMarkerClick(Marker marker) {
Marker.setIcon (BitmapDescriptor icon);
}
来源:https://stackoverflow.com/questions/14449870/change-marker-icon-during-runtime