2

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!

Hamy

Hamy
  • 20,662
  • 15
  • 74
  • 102
  • I seem to be having difficulties adding multiple overlay items to the map view. I declare my own ItemizedOverlay class (pretty much the same as in the MapView tutorial on the dev site) but I only see the first marker added. A more thorough description of the problem is on http://www.anddev.org/multiple_overlay_items-t12171.html Can anyone help me with this? Thanks – Bostjan Mar 19 '10 at 18:13

2 Answers2

3

as mentioned in this article

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

setLastFocusedIndex(-1);

populate();

Example:

@Override
protected OverlayItem createItem(int i) {
    return overlays.get(i);
}
protected void removeOverlay(OverlayItem o){
    overlays.remove(o);
    setLastFocusedIndex(-1);
    populate();
}
@Override
public int size() {
    return overlays.size();
}
public void addOverlay(OverlayItem o){
    overlays.add(o);
    setLastFocusedIndex(-1);
    populate();
}
0

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