How to make a mutable ItemizedOverlay
I would like to make a Google map overlay with changable pins. An easy way开发者_C百科 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
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();
}
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
精彩评论