WeathForc WeathForc - 1 year ago 149
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

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);

Following is updateUi method:

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

Polyline pl = new Polyline();
List<GeoPoint> pts = new ArrayList<>();

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 Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download