I am trying to animate my marker instead of making it jump between the 2 points. For some reason the animation does not work.
Every time I get a new current location, I call the below code.
if (currentLatitude != 0 && currentLongitude != 0) {
String actualRideStartTime = "";
if (currentRideTracking.getActualRideStartTime() != 0) {
actualRideStartTime = TIME_FORMAT.format(currentRideTracking
.getActualRideStartTime());
}
vehicleLocation = new LatLng(currentLatitude, currentLongitude);
markerOptions = new MarkerOptions().
.position(vehicleLocation);
animateMarker(map.addMarker(markerOptions), vehicleLocation, false);
// builder.include(vehicleLocation);
}
AnimateMarker method
public void animateMarker(final Marker marker, final LatLng toPosition,
final boolean hideMarker) {
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 long duration = God.DRIVER_LOCATION_UPDATE_FREQUENCY;
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));
Log.d(God.LOG_TAG, ">"+lat+"<"+lng) ;
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
marker.showInfoWindow();
}
}
}
});
}
I tried commenting out // builder.include(vehicleLocation);
but yet the smooth movement dont happen, instead it jumps ahead just like it used to.
Edit : Does the frequency of updates matter ? I am using googleapiclient
, so I dont know how often the updates come. Maximum wait time has been set to God.DRIVER_LOCATION_UPDATE_FREQUENCY
, which is used in the duration
in the animateMarker
method.