WeathForc WeathForc - 1 month ago 22
Android Question

Osmdroid marker not updating

I have to make implement navigation using osmdriod maps api. The Activity receives broadcasts from a location service, using following receiver:

public class ReceiveMessages extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
String lat = intent.getStringExtra("Latitude");
String lon = intent.getStringExtra("Longitude");
System.out.println("on recieve called "+lat+lon);
updateUi(Double.parseDouble(lat),Double.parseDouble(lon));
}
}


Following is updateUi method:

public void updateUi(Double lat,Double lon) {
gPt1 = new GeoPoint(lat, lon);
Marker endMarker = new Marker(mMapView);
endMarker.setPosition(gPt1);
endMarker.setTitle("Current Position"+lat+" "+lon);
mMapView.getOverlays().add(endMarker);

Polyline pl = new Polyline();
pl.setWidth(5f);
pl.setColor(Color.RED);
List<GeoPoint> pts = new ArrayList<>();
pts.add(gPt0);
pts.add(gPt1);
pl.setPoints(pts);
pl.setGeodesic(true);
mMapView.getOverlayManager().add(pl);
mMapView.invalidate();
}


Now what is happening is that the current position markers are getting added one above the other as and when broadcast receiver gets called when current position changes like here, here.

I just want a single current position marker and a single poly line should be present between the 2 markers, which get updated.

Answer

If you add a Marker and Polyline at each update, the API does as requested: they are added.

2 solutions, depending on your context:

1) If you always have a Marker and a Polyline to show => add them to the MapView only once, at the beginning. Then, on update, just update them (and invalidate the mapview). But don't add them again and again.

2) If the list of overlays is subject to change at each update (n Markers, m Polylines): as suggested by spy, remove all overlays, and recreate them. Using a FolderOverlay can be very helpful to manage this operation safely (if you have other overlays that you want to keep all the time).