Без удаления маркера обновите новую позицию на карте Google ()
1.начально созданный маркер:
Double lang = ds.child("lang").getValue(Double.class);
Double lat = ds.child("lat").getValue(Double.class);
LatLng latLng=new latLng(lang ,lat )
// Creating a marker
Marker marker= mMap.addMarker(new MarkerOptions()
.position(latLng)
.draggable(true));
2. при получении обновленного местоположения
LatLng oldLocation, newLocaation;
float bearing = (float) bearingBetweenLocations(oldLocation, newLocaation);
rotateMarker(marker, bearing);
private double bearingBetweenLocations(LatLng perviousPos,LatLng newPos) {
double PI = 3.14159;
double lat1 = perviousPos.latitude * PI / 180;
double long1 = perviousPos.longitude * PI / 180;
double lat2 = newPos.latitude * PI / 180;
double long2 = newPos.longitude * PI / 180;
double dLon = (long2 - long1);
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
* Math.cos(lat2) * Math.cos(dLon);
double brng = Math.atan2(y, x);
brng = Math.toDegrees(brng);
brng = (brng + 360) % 360;
return brng;
}
.To rotate marker using above bearing angle i have used this code
Here isMarkerRotating is a boolean value. Add isMarkerRotating = false in OnCreate
method
private void rotateMarker(final Marker marker, final float toRotation) {
if(!isMarkerRotating) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = marker.getRotation();
final long duration = 2000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
isMarkerRotating = true;
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed / duration);
float rot = t * toRotation + (1 - t) * startRotation;
float bearing = -rot > 180 ? rot / 2 : rot;
marker.setRotation(bearing);
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
isMarkerRotating = false;
}
}
});
}
}