I have one doubt but this may be answered many time but i couldn't find any solution regarding this. My doubt is how to animate marker over polyline like uber do now let me explain in detail, am getting lat long from server frequently based on this i need remove travelled portion of polyline and animate over it, now let me post what i have tried so far.
This is method where i get lat long frequently
public void UpdatePolyline(Double lat_decimal,Double lng_decimal){
List<LatLng>removedline=new ArrayList<>();
float distance=60.0f;
if (polylineLatlng.size()>0){
for (int i = 0; i < polylineLatlng.size(); i++) {
if (polylineLatlng.size()>0) {
try {
float smallest_distance=CalculationByDistance(polylineLatlng.get(i),new LatLng(lat_decimal, lng_decimal));
if (smallest_distance<distance){
removedline.add(polylineLatlng.get(i));
polylineLatlng.remove(i);
distance=smallest_distance;
}
}catch (Exception ex){
ex.printStackTrace();
break;
}
}
else {
break;
}
}
}
if (mpolyline != null) {
if (polylineLatlng.size() > 0) {
mpolyline.setPoints(polylineLatlng);
for (int i=0;i<removedline.size();i++){
Location location = new Location("");
location.setLatitude(removedline.get(i).latitude);
location.setLongitude(removedline.get(i).longitude);
setDriverMarker(location);
}
}
else {
Location location = new Location("");
location.setLatitude(lat_decimal);
location.setLongitude(lng_decimal);
setDriverMarker(location);
}
}}}
private void setDriverMarker(Location latLng) {
if (latLng != null) {
if (markerDriver == null) {
RideMarker(latLng.getLatitude(),latLng.getLongitude());
} else {
move(markerDriver,latLng);
}
}}
public void move( final Marker marker, final Location toPosition) {
if (fromPosition != null && marker != null && toPosition != null) {
final Handler handlerRotation = new Handler();
final long startAngle = SystemClock.uptimeMillis();
final float startRotation = marker.getRotation();
final long durationRotation = 300;
final Interpolator interpolatorRotation = new LinearInterpolator();
float bearing = fromPosition.bearingTo(toPosition);
// Print.e("Bearing:" + bearing);
angle = bearing<0?(360+bearing):bearing;
angle = angle%360f;
// Print.e("Angle:" + angle);
handlerRotation.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - startAngle;
float t = interpolatorRotation.getInterpolation((float) elapsed / durationRotation);
float rot = t * angle + (1 - t) * startRotation;
float mAngle = -rot > 180 ? rot / 2 : rot;
marker.setRotation(mAngle);
if (t < 1.0) {
handlerRotation.postDelayed(this, 16);
} else {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection projection = map.getProjection();
Point startPoint = projection.toScreenLocation(marker.getPosition());
final LatLng startLatLng = projection.fromScreenLocation(startPoint);
final long duration = 1000;
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.getLongitude() + (1 - t)
* startLatLng.longitude;
double lat = t * toPosition.getLatitude() + (1 - t)
* startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
map.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
.target(new LatLng(toPosition.getLatitude(),toPosition.getLongitude()))
.zoom(map.getCameraPosition().zoom)
.build()));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
}
}
});
}
fromPosition = toPosition;
}
But i couldn't animate marker over it and even travelled portion of polyline is not removing how to do this please anyone share this am struggling with this Thanks advance!!