3

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?

DieselPower
  • 738
  • 2
  • 6
  • 22

2 Answers2

13

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()
    .enabled(false)
    .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)
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • I even asked you something a few days before via the issue-tracker ;) My problem is that i´m using the new android studio and need to include the library via gradle ;) I can do it manually for now, but it would be really nice if i only had to add one or two lines in a gradle-file and the dependencies are managed automatically ... it isn´t good practice to add a library directly and even check it into vcs... but for now i think i have to go with it :) – DieselPower Aug 27 '13 at 05:48
  • Hey Maciej, i managed to import your library into an android studio project ;) But one question, what is the best position for adding markers? I added markers (without your library) depending on the visible area (always fetch from local database) and limit to 40 markers ... Should i read one time all databaseentries and let your library do the rest? Or should i now also only show visible markers? – DieselPower Aug 27 '13 at 08:36
  • @DieselPower I plan to add the gradle support sooner or later. As to loading, it should be best to leave the handling to the library. If you use more than few hundred, I suggest using `addMarkersDynamically` to better UX. – MaciejGórski Aug 27 '13 at 10:36
  • Just thought I'd add: dependencies { compile 'com.androidmapsextensions:android-maps-extensions:2.2.0' compile 'com.google.android.gms:play-services-maps:8.1.0' } – jasonflaherty Dec 06 '15 at 05:25
  • If you're migraiting from Google Maps, do you have to update Marker & MarkerOptions logic as well in order to take advantage of "addMarkersDynamically" or is just specifying enough? – AlexVPerl Dec 22 '15 at 06:38
  • 1
    @AlexVPerl When using `addMarkersDynamically`, you simply add all the markers via `GoogleMap.addMarker`, but AME doesn't really add them unless it's necessary (they would be on user screen). Of course `Marker` and `MarkerOption` have to be from AME package instead of Maps Android API. – MaciejGórski Dec 22 '15 at 10:57
-1
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                MarkerOptions marker = new MarkerOptions().position(
                        latLng)
                        .title("Hello Maps ");
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
                googleMap.addMarker(marker);
            }
        });
Vahid Rajabi
  • 191
  • 3
  • 3