I was following this tutorial to create something a little diffrent, this tutorial showed how the overlay works with predefined items inserted to it, but what if i want to dynamiclly add items to it? then I must start with an empty overlay:
Instead of :
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
ItemOverlay itemizedoverlay = new ItemOverlay(drawable, this);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
which works just like as expected in the tutorial, I need to put this:
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
ItemOverlay itemizedoverlay = new ItemOverlay(drawable, this);
mapOverlays.add(itemizedoverlay);
which ommits the initializition of the overlay with an item.
this approach leads to the map getting stuck, and after few taps my app gets kicked out due to null exception.
1.is this expected behavior? it seems like a bug to me…
2.what do i do in order to acheive the expected behavior?(in which i can start with an empty overlay and add items to it dynamically)
And as for what i want to acheive, which is to not initiate the overlay with and item but to use addOverlay dynamically, this way i can
As far as I understand, under
ItemOverlayyou meanItemizedOverlay. If so, you are on the right track.ItemizedOverlayis a container of map items, so every time you add/delete items fromItemizedOverlayyou need to set last focused item to-1and callpopulate()method to notify mapview about content change.Just remember that you need to make changes in
ItemizedOverlaycontent in a UI thread context 😉There is also undocumented trick here. You need to set last focused index to
-1and dopopulate()even when you don’t have items (so you need to do this on initialization stage)So your code should look like: