How to dynamically update a specific overlayitem

I am developing a location-based Android application that deals with many overlay elements.

Background: There are quite a few objects that I want to display on the map. The coordinates of objects change in real time as their location changes. In accordance with the updated location, objects must be updated on the map. Entities can grow to a large number.

Implementation I have two overlays. One for MyLocationOverlay to display the user's location, and the other a subclass of ItemizedOverlay with all the objects that I need to display on the map.

Problem Whenever the location of an object changes, the corresponding overlayitem must be updated. The problem is that I'm not sure if this is the best way to achieve this. I have these options

  • When I get the location of a specific object, I delete all overlays from the map and re-add overlays. One of the overlayitems is now created with an updated location.

    List<Overlay> overlays = map.getOverlays(); overlays.removeAll(overlays); overlays.add(new MyOverlay(marker,this)); 
  • I create an Overlay for each object. Value I will create 500 overlays, each containing only one overlay element. I create a subclass of the Overlay class and add my own properties to clearly identify the entity and create it for everyone. When I achieve an update in the location of a specific object, I repeat and get a specific overlay element, delete it and add a new one with an updated location.

I do not know which one is the best to use.

I feel that deleting all and re-adding all overlays for each location update (which can be quite common) of each individual object becomes quite expensive when the number of objects becomes more than 500 or 1000.

At the same time, iterating through the same number of objects can be the same invoice.

Any advice on which to choose one or the best way to implement this will be appreciated.

/Andy

+6
source share
3 answers

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

+1
source

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

In any case, before adding them to the map, upload them to another stream, and after all the images and data have been downloaded, add them to the map in the main topic.

0
source

I suggest you store your overlayItems objects in a HashSet with your entities as keys. When an object is updated, you can create a new OverlayItem and save it in a set. Since this is a collection, the old OverlayItem will be deleted.

To display OverlayItems, use ItemizedOverlay. In the ItemizedOverlay.createItem(int position) method, you can get OverlayItem from the set. Remember to override the ItemizedOverlay.getSize() method with return set.size() .

With this call, populate() on ItemizedOverlay every time you update a collection.

Take a look at this Tutorial for more information or post a comment if you have questions.

0
source

Source: https://habr.com/ru/post/920794/


All Articles