I am doing map clustering in Android Google maps v2. I just want to animate the marker from one geopoint to another. Is there a way to move a marker in Google maps v2?
Move markers in google map v2 android
Asked Answered
There's one example of moving marker in google map v2 demo app .. in the sample of the play library!!
I have looked into that!! here the code for moving an marker : -- >
public void animateMarker(final Marker marker, final LatLng toPosition,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = mGoogleMapObject.getProjection();
Point startPoint = proj.toScreenLocation(marker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 500;
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);
double lng = t * toPosition.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * toPosition.latitude + (1 - t)
* startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
Hope it help every one!!
This is in file adt-bundle-linux/sdk/extras/google/google_play_services/samples/maps/src/com/example/mapdemo/MarkerDemoActivity.java if anyone is interested (in 4.2.2 examples) –
Carnahan
final Interpolator interpolator = new LinearInterpolator();----- Giving error --- "Incompatible Type" ----- How to fix ? –
Pilarpilaster
Hi how to animate the marker from one geopoint to another when we have multiple points. –
Rodolphe
@Rodolphe This above function is for animation between any point to other point. –
Carlynne
@anuragshrivastava Marker is something which is drawn on MapView –
Carlynne
@SandeepDhull thank you. but i completed that project. –
Deportment
How can I get mGoogleMapObject? –
Attu
@GoogleMapObject is with a Map. You need to Figure out how to get a reference to it. –
Carlynne
marker is not moving with the above code @SandeepDhull –
Ilene
@PuneetKansal the above code is 7 years old. Please try other replies –
Carlynne
@SandeepDhull Thanks, buddy, I finally found it! –
Pyromagnetic
@kdblue This answer should not be valid anymore, its been 7years –
Carlynne
@SandeepDhull its not about time, it's about code you have written and it's moving my marker smoothly. –
Pyromagnetic
Proper way with ValueAnimator :
double[] startValues = new double[]{marker.getPosition().latitude, marker.getPosition().longitude};
double[] endValues = new double[]{destLatLng.latitude, destLatLng.longitude};
ValueAnimator latLngAnimator = ValueAnimator.ofObject(new DoubleArrayEvaluator(), startValues, endValues);
latLngAnimator.setDuration(600);
latLngAnimator.setInterpolator(new DecelerateInterpolator());
latLngAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
double[] animatedValue = (double[]) animation.getAnimatedValue();
marker.setPosition(new LatLng(animatedValue[0], animatedValue[1]));
}
});
latLngAnimator.start();
DoubleArrayEvaluator
does not exist in android.animation
package, there is my implementation :
import android.animation.TypeEvaluator;
/**
* Inspired from {@link android.animation.FloatArrayEvaluator}
* <p/>
* This evaluator can be used to perform type interpolation between <code>double[]</code> values.
* Each index into the array is treated as a separate value to interpolate. For example,
* evaluating <code>{100, 200}</code> and <code>{300, 400}</code> will interpolate the value at
* the first index between 100 and 300 and the value at the second index value between 200 and 400.
*/
public class DoubleArrayEvaluator implements TypeEvaluator<double[]> {
private double[] mArray;
/**
* Create a DoubleArrayEvaluator that does not reuse the animated value. Care must be taken
* when using this option because on every evaluation a new <code>double[]</code> will be
* allocated.
*
* @see #DoubleArrayEvaluator(double[])
*/
public DoubleArrayEvaluator() {
}
/**
* Create a DoubleArrayEvaluator that reuses <code>reuseArray</code> for every evaluate() call.
* Caution must be taken to ensure that the value returned from
* {@link android.animation.ValueAnimator#getAnimatedValue()} is not cached, modified, or
* used across threads. The value will be modified on each <code>evaluate()</code> call.
*
* @param reuseArray The array to modify and return from <code>evaluate</code>.
*/
public DoubleArrayEvaluator(double[] reuseArray) {
mArray = reuseArray;
}
/**
* Interpolates the value at each index by the fraction. If
* {@link #DoubleArrayEvaluator(double[])} was used to construct this object,
* <code>reuseArray</code> will be returned, otherwise a new <code>double[]</code>
* will be returned.
*
* @param fraction The fraction from the starting to the ending values
* @param startValue The start value.
* @param endValue The end value.
* @return A <code>double[]</code> where each element is an interpolation between
* the same index in startValue and endValue.
*/
@Override
public double[] evaluate(float fraction, double[] startValue, double[] endValue) {
double[] array = mArray;
if (array == null) {
array = new double[startValue.length];
}
for (int i = 0; i < array.length; i++) {
double start = startValue[i];
double end = endValue[i];
array[i] = start + (fraction * (end - start));
}
return array;
}
}
I have done in this way:
private Marker marker;
While adding marker on Google Map:
if(marker!=null){
marker.remove();
}
LatLng mLatLng = new LatLng(Your_Latitude, Your_Longitude);
marker = mMapHistory.addMarker(new MarkerOptions().position(mLatLng).title("My Title").snippet("My Snippet").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
Hope this easiest way will help you.
This would simply update the location of marker with a new one but does not animate the marker on the map. –
Endplay
Please try this:
private static void addMarkerWithCameraZooming(Context ctx, GoogleMap googleMap, double latitude, double longitude, String title, boolean dragabble) {
LatLng current_latlng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(current_latlng)
.title(title)
.snippet(getLocality(current_latlng, ctx))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.person_marker))
.draggable(dragabble)
);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).tilt(30).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
mGoogleMap.setOnMarkerDragListener(new OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker markerDragStart) {
// TODO Auto-generated method stub
if (BuildConfig.DEBUG)
Log.i("Marker drag", "start");
}
@Override
public void onMarkerDragEnd(Marker markerDragEnd) {
if (BuildConfig.DEBUG)
Log.i("Marker drag", "start");
}
@Override
public void onMarkerDrag(Marker markerDrag) {
if (BuildConfig.DEBUG)
Log.i("Marker drag", "start");
}
});
Hi I had the same problem, here my post Google Maps Android api v2 and current location
you can use remove() method of Marker to delete the old marker with the new one. i hope it's what you need
© 2022 - 2024 — McMap. All rights reserved.