i am trying to place a marker to a map according to location, and my activity refreshes after every 10 sec so that I can get location from database every 10.
What I am expecting is to change marker location every 10 sec but marker keeps on changing position frequently that is 5 to 6 times every 10 sec.
And my cab marker also keeps on dissapearing I this its the map.clear(), but without it 2 to 4 markers keeps on reappearing
For placing markers
LatLngBounds.Builder builder = new LatLngBounds.Builder();
ArrayList<Marker> markers = new ArrayList<>();
mMap.clear(); // to clear markers and polylines
drivLat1 = driverLocation.getLatitude();
drivLng1 = driverLocation.getLongitude();
drivLat2 = driverChangedLocation.getLatitude();
drivLng2 = driverChangedLocation.getLongitude();
bearingBetweenLocations(drivLat1,drivLng1,drivLat2,drivLng2);
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.car_marker);
markers.add(0,mMap.addMarker(new MarkerOptions().position(new LatLng(driverLocation.getLatitude(), driverLocation.getLongitude()))
.icon(icon)
.title("Driver Location")));
markers.add(1,mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("User Location")));
rotateMarker(markers.get(0), (float) bearingBetweenLocations(drivLat1,drivLng1,drivLat2,drivLng2));
for (Marker marker : markers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
int padding = 100;
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
private double bearingBetweenLocations(double lat11 ,double long11,double lat22 ,double long22) {
double PI = 3.14159;
double lat1 = lat11 * PI / 180;
double long1 = long11 * PI / 180;
double lat2 = lat22 * PI / 180;
double long2 = long22 * 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;
}
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 = 1000;
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;
marker.setRotation(-rot > 180 ? rot / 2 : rot);
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
isMarkerRotating = false;
}
}
});
}
}
And this is how I set accuracy, bcoz I think that this can be a problem
if (location.hasAccuracy()) {
// Accuracy is in rage of 20 meters, stop listening we have a fix
if (location.getAccuracy() < 20) {
LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
}
}
What I want is that markers only change position when data gets updated