How to make a mutable ItemizedOverlay

I would like to make a Google map overlay with changable pins. An easy way to visualize this would be to think of a near real time overlay, where the pins are constantly changing location.

However, I can't seem to think of a safe way to do this with the ItemizedOverlay. The problem seems to be the call to populate - If size() is called by some maps thread, and then my data changes, then the result when the maps call accesses getItem() can be an IndexOutOfBoundsException.

Can anyone think of a better solution than overloading populate and wrapping super.populate in a synchronized block?

Perhaps I could get better luck using a normal Overlay? The Itemized one seems to exist to manage the data for you, perhaps I am making a fundamental mistake by using it?

Thanks for any help, my brain is hurting!


Asked by: Oliver384 | Posted: 25-01-2022

Answer 1

as mentioned in this article

You need to call the following after adding or removing an item from the list.




protected OverlayItem createItem(int i) {
    return overlays.get(i);
protected void removeOverlay(OverlayItem o){
public int size() {
    return overlays.size();
public void addOverlay(OverlayItem o){

Answered by: Miranda104 | Posted: 26-02-2022

Answer 2

I had a similar problem and solved by mutually excluded (yes... making them synchronized) the method size and the method that update (add/change) the pins... In fact, the first was called bye the GUI thread while the second is in a async worker so it is possible that they are called asynchronously

Answered by: Rubie661 | Posted: 26-02-2022

Similar questions

java - Adding visible "Markers" to represent Geopoints to a MapView using ItemizedOverlay in Android

I am building an application which stores GPS locations in a SQLite database and then outputs the data onto a MapView using an Overlay by drawing a red line between the points. I want to be able to show graphical markers (images) for each of these points as well as the red line. My code is as follows: public class MyOverlay extends ItemizedOverlay<OverlayItem> { // private Projection project...

how to handle the visibility of the ItemizedOverlay in mapview in android?

i want set the visibility to itemized overlay in map view. if the zoom level is less than 10 i want to set visibility of overlay is GONE else VISIBLE. how to do that? and also cant set zoom level Listener.

android - Performance of map overlay in conjunction with ItemizedOverlay is very poor

I am trying to display one png (drawable) on a map in about 300 points. I am retrieving the coordinates from a Sqlite table, dumping them in a cursor. When I try to display them by parsing through the cursor, it takes for ever for the images to be drawn, about .5 second per image. I find that to be suspiciously slow, so some insight on how I can increase performance would help. Here is the snippet of my code that does the ...

android - ItemizedOverlay seems to be drawing with a "broken" projection

I have a strange problem that I don't really know how to attack so I'm wondering if someone has had a similar problem before. I override the draw(Canvas canvas, MapView mapView, boolean shadow) method in an ItemizedOverlay subclass to draw some paths, text etc. between the items. The first thing I do in the override is call super.draw with all these parameters. When I start the app, the lines that I draw in my over...

android - Differences between ItemizedOverlay and Overlay class

Can someone tell my when to use Overlay or when to use ItemizedOverlay class! What are differences between this two classes? Draw methods do the same thing? Can I use only ItemizedOverlay class in my project or I must use and Overlay as base class! Thanks

android - How can I start and Activity from a class extending ItemizedOverlay?

I have a class that extends ItemizedOverlay. In it, I have: @Override protected boolean onTap(int index) { OverlayItem item = mOverlays.get(index); /*// working below code, just replace */ AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.setPositiveButton("Details", new DialogInterface.OnCli...

google maps - Android ItemizedOverlay draw method question

Does anybody know the relationship between the members of an Android Itemized Overlay list and when draw() is called. Specifically, I'm trying to find out if draw is called once for each Overlay or once only for the whole set. Thanks, R.

android - Increase Hit detection on ItemizedOverlay

I have an itemizedOverlay extended and I want to increase the hit detection on the OverLayItem's if this is possible?

google maps - Accessing an Android View from extended ItemizedOverlay?

I am creating an extension of ItemizedOverlay. When a user taps a pin on the map (onTap) I want to display a RelativeLayout, which is included in the same .xml file as the map. When the RelativeLayout is visable, it covers the map. Since this view is the context for the main class, I do not know how to access the view (DetailLayout) from within the onTap() call. <?xml version="1.0" encoding="utf...

android - Problem drawing into an ItemizedOverlay MapView on Satellite mode

i subclass the ItemizedOverlay to draw a custom graphics for my items, the problem is that i have the MapView and my custom overlay with a List items... when i construct the items, populate and draw in the "map mode" (setSatellite(false)) works good, but when i change the mode to setSatellite(true) my custom overlay holds all the items but not refresh, invalidate, redraw the map, i try invoking all those methods and nothin...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android