Search code examples
javaandroiddictionaryopenstreetmaposmdroid

How notify current layer for position change


I'm currently using osmdroid to display current positioning.

Based on the following example i tried to optimize the system a little bit by not constructing the ItemizedOverlay<OverlayItem> and ArrayList<OverlayItem> each time my location is changed, but construct them only once in the constructor, and later on simply add points to my ArrayList variable.

Here's how it looks now:

private void InitializeMarkersOverlay() {
    mOverlayItemArrayList = new ArrayList<OverlayItem>();
    ItemizedOverlay<OverlayItem> locationOverlay =
            new ItemizedIconOverlay<OverlayItem>(this, mOverlayItemArrayList, null);

    mMapView.getOverlays().add(locationOverlay);
}

and when a new location arrives:

private void AddPointToOverlay(GeoPoint gPt, boolean bShouldClearList) {

    OverlayItem overlayItem = new OverlayItem("", "", gPt);
    Drawable markerDrawable = ContextCompat.getDrawable(this, R.drawable.pin);
    overlayItem.setMarker(markerDrawable);

    // first time initializer
    if(bShouldClearList) {
        mOverlayItemArrayList.clear();
    }
    mOverlayItemArrayList.add(overlayItem);
}

Since my mMapView already has a pointer to mOverlayItemArrayList i was hoping that my mapview's layer would be automatically notified regarding the change. but nothing actually happens. Only by recreating the objects, i get to see the pin.


Solution

  • I got it working by accessing the overlay directly from the mapview object, not sure why exactly, as i was hoping mMapView.getOverlays() would hold a reference to the ItemizedIconOverlay and its itimized array

    if(mMapView.getOverlays().size() > 0) {
                ((ItemizedIconOverlay<OverlayItem>)mMapView.getOverlays().get(0)).removeAllItems();
                ((ItemizedIconOverlay<OverlayItem>)mMapView.getOverlays().get(0)).addItem(overlayItem);
            }
        }