DieselPower DieselPower - 1 year ago 382
Android Question

Add markers dynamically on Google Maps v2 for Android

what are the possibilities to add markers dynamically depending on the area of the map which is shown?

I have a lot of markers or let´s say my markers need a lot of performance to be rendered, cause they are custom. I implemented it now, that only 40 markers are drawn when a "camerachange" was fired. So at this time i always render 40 new markers when cameraposition was changed.

I read about runnable and handler, are their more options? Does someone know which of these possibilities is the best, so that ui-thread isn´t blocked?

Answer Source

I would suggest using one of clustering libraries available for Google Maps Android API v2.

Android Maps Extensions apart from clustering can do a lot of work for you. The API is very similar to official Google library.
If you don't want to use clustering at all, you can still achieve your goal with:

map.setClustering(new ClusteringSettings()
    .addMarkersDynamically(true)); // I didn't change the API to match your title ;)

Clusterkraf mainly focuses on animated clustering.

Android Maps Utils as of this writing doesn't have clustering merged into the main branch, but you can see clustering branch.

If you want to code it yourself, I can suggest these approaches:

  • adding Markers only in VisibleRegion (same thing Android Maps Extensions does when using code above), example from my demo project here: AddOnlyVisibleMarkersExampleActivity.java
  • adding Markers using Handler, example here: AddMarkersInBackgroundExampleActivity.java
  • mix of the above; this can help a lot with app responsiveness when there are potentially hundreds of markers on screen (which you should avoid by using clustering)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download