Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically update a specific overlayitem

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

  1. 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));
    
  2. 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

like image 610
andy Avatar asked Jul 18 '12 11:07

andy


1 Answers

I believe the best solution (regarding performance) is to create a custom Overlay class that inherits from Overlay class so that you can override the onDraw method the way you want. See this previous SO post to get an idea of how to do it. If you want to avoid that, you can check the ItemizedOverlay, if you have not done that already. It is more efficient than creating an Overlay for each entity, however not as efficient as the first approach suggested.

like image 159
Angelo Avatar answered Nov 20 '22 20:11

Angelo