Reputation: 1489
I am getting Updated LATLNG in every 5 sec and I want to update marker on updated Latlng here is my code :
private class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
lat = Double.valueOf(intent.getStringExtra("lat"));
longD = Double.valueOf(intent.getStringExtra("long"));
userLat = Double.valueOf(intent.getStringExtra("userLat"));
userLong = Double.valueOf(intent.getStringExtra("userLong"));
latDest = Double.valueOf(intent.getStringExtra("latDest"));
longDest = Double.valueOf(intent.getStringExtra("longDest"));
userDropLocation = intent.getStringExtra("userDropLocation");
userManualLocation = intent.getStringExtra("userLocation");
driverLatLng = new LatLng(lat, longD);
userLatlong = new LatLng(userLat, userLong);
dropLatlong = new LatLng(latDest, longDest);
Log.d("@@Latdriver", String.valueOf(lat));
Log.d("@@longDriver", String.valueOf(longD));
Log.d("@@latLngCurrent", String.valueOf(latLngCurrent));
Log.d("@@driverLatLng", String.valueOf(driverLatLng));
Log.d("@@userLat", String.valueOf(userLat));
Log.d("@@userLong", String.valueOf(userLong));
Log.d("@@userManualLocation", userManualLocation);
Log.d("@@userDropLocation", userDropLocation);
String urlToDrop = getDirectionsUrl(driverLatLng, dropLatlong);
DownloadTask downloadTask1 = new DownloadTask();
downloadTask1.execute(urlToDrop);
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_green))
.position(new LatLng(latDest,longDest )));
MarkerOptions a = new MarkerOptions()
.position(driverLatLng);
Marker m = map.addMarker(a);
m.setPosition(driverLatLng);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(driverLatLng);
LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 150);
map.animateCamera(cu);
}
}
In the above code It is adding every time new marker on updated along how to resolve this please help me, Thank you in advance I would appreciate every answer here
Upvotes: 0
Views: 39
Reputation: 4633
Every 5 secs is too frequent. it will drain user's battery like anything. 30-60 seconds should be fine.
problem is here
MarkerOptions a = new MarkerOptions()
.position(driverLatLng);
Marker m = map.addMarker(a);
m.setPosition(driverLatLng);
you are creating new Marker every time .
Use field variable for a marker . initialize it for the first time. and from next time onwards just set the position of a marker to new lat lng.
your code should be something like this
private Marker driverMarker ;
// inside broad cast receiver
if(null==driverMarke){
MarkerOptions a = new MarkerOptions()
.position(driverLatLng);
driverMarker = map.addMarker(a);
}
driverMarker.setPosition(driverLatLng);
Upvotes: 1