How to dynamically update a specific overlayitem
Asked Answered
S

3

6

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

Stanzel answered 18/7, 2012 at 11:19 Comment(0)
C
1

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.

Cupreous answered 20/7, 2012 at 16:49 Comment(2)
So you suggest that creating a single custom overlay class and have all my entities drawn on that? Which one will have a better performance - a custom overlay class as you have suggested with all 500 entities or similar custom overlay class, one for each entity (there will be 500 objects of custom overlay class)Stanzel
I believe there is no definite answer since that depends on the implementation of the custom overlay class. If i wanted to be sure for the perfomance, i would implement a simple test case for these two appraches and see which of this has the best performance.Cupreous
H
0

I had the same problem, and what I did is your first choice.

Anyway, before adding them into the map, load them on a different Thread, and after all of the Images and datas have been loaded, add them on the map on the main Thread.

Hu answered 27/7, 2012 at 11:54 Comment(0)
U
0

I suggest that you store your overlayItems in a HashSet with your entities as keys. When an entity is updated you can create a new OverlayItem and store it in the set. As it is a set the old OverlayItem will be removed.

To show the OverlayItems use a ItemizedOverlay. In the ItemizedOverlay.createItem(int position) method you can get the OverlayItem of the set. Do not forget to override the ItemizedOverlay.getSize() method with return set.size().

With that call populate() on the ItemizedOverlay each time you updated the set.

Look at this Tutorial for more info or put a comment if you have questions.

Unintelligible answered 27/7, 2012 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.