How to Animate Marker in Android Map API V2

Animating markers on Google Maps v2

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.

Animate the rotation of the Marker in google map v2

static public void rotateMarker(final Marker marker, final float toRotation, GoogleMap map) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = marker.getRotation();
final long duration = 1555;

final Interpolator interpolator = new LinearInterpolator();

handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed / duration);

float rot = t * toRotation + (1 -t) * startRotation;

marker.setRotation(-rot > 180 ? rot/2 : rot);
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
}

i have managed to do it :)

Map Marker icon animation (Google Maps API V2 for Android)

I'm afraid there is no way to do it today without actually removing the marker and adding it again with another drawable. I have just done so myself in an attempt to create a progress bar. It works, but it is an awkward way of doing it.

Click here for a good explanation in another thread.

Android Google Maps v2: animate marker size

You may try something like this

final Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_temperature_kelvin_black_48dp);
final Bitmap target = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(target);
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.setDuration(500);
animator.setStartDelay(1000);
final Rect originalRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF scaledRect = new RectF();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float scale = (float) animation.getAnimatedValue();
scaledRect.set(0, 0, originalRect.right * scale, originalRect.bottom * scale);
canvas.drawBitmap(bitmap, originalRect, scaledRect, null);
marker.setIcon(BitmapDescriptorFactory.fromBitmap(target));
}
});
animator.start();

Google Map v2 Marker Animation

I found a solution that worked for me:

final LatLng target = NEW_LOCATION;

final long duration = 400;
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = map.getProjection();

Point startPoint = proj.toScreenLocation(marker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);

final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
if (elapsed > duration) {
elapsed = duration;
}
float t = interpolator.getInterpolation((float) elapsed / duration);
double lng = t * target.longitude + (1 - t) * startLatLng.longitude;
double lat = t * target.latitude + (1 - t) * startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
// Post again 10ms later.
handler.postDelayed(this, 10);
} else {
// animation ended
}
}
});

Google Map v2 Marker Animation

I found a solution that worked for me:

final LatLng target = NEW_LOCATION;

final long duration = 400;
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = map.getProjection();

Point startPoint = proj.toScreenLocation(marker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);

final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
if (elapsed > duration) {
elapsed = duration;
}
float t = interpolator.getInterpolation((float) elapsed / duration);
double lng = t * target.longitude + (1 - t) * startLatLng.longitude;
double lat = t * target.latitude + (1 - t) * startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
// Post again 10ms later.
handler.postDelayed(this, 10);
} else {
// animation ended
}
}
});

Add animation to default marker on setOnMarkerClickListener maps api v2 android

I have sorted out the solution by changing the onmarkerclick code as below...

  public boolean onMarkerClick(Marker marker) {
String s= marker.getTitle();
String snippet= marker.getSnippet();
markerlat= marker.getPosition().latitude;
markerlong= marker.getPosition().longitude;
if(prevmarker!=null){
if(!prevmarker.getTitle().equals(getActivity().getResources().getString(R.string.here)))
{
prevmarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
}
}
if (!s.equals(getActivity().getResources().getString(R.string.here))) {
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}


Related Topics



Leave a reply



Submit