I am developing an location based android app which deals with lot of overlay items.
Background: There are quite a few entities that I want to display over a map. The co-ordinates of the entities change in real time as their location change. Corresponding to the updated location, the entities shall be updated on the map. The entities can grow to a large number.
The Implementation I have two overlays. One for MyLocationOverlay to display the user location and the other, an ItemizedOverlay subclass with all entities that I need to display on map.
The Problem Whenever the location of an entity is changed, the corresponding overlayitem needs to be updated. The problem is that I'm not sure which is the best way to accomplish this. I have these choices
As I receive a location of a particular entity, I remove all the overlays from the map and re-add the overlays. One of the overlayitems is now created with updated location.
List<Overlay> overlays = map.getOverlays(); overlays.removeAll(overlays); overlays.add(new MyOverlay(marker,this));
I create an Overlay for each entity. Meaning I will create 500 overlays, each containing just one overlay item. I create a sub class of Overlay class and add my own properties to distinctly identify the entity and create one for each. When I reach the location update of a particular entity, I iterate and get the specific overlay item, remove it and add a new one with updated location.
I don't know which is the best one to use performance wise.
I feel removing all and re-adding all overlays for every single location update (which can be quite often) of every single entity becomes quite an overhead when the number of entities become more than 500 or 1000.
At the same time, iterating through the same number of entities can be equal overhead too.
Any advice on which one to choose from or a better way to implement this would be appreciated.
/andy