Add markers dynamically on Google Maps v2 for Android

别说谁变了你拦得住时间么 提交于 2019-11-27 20:20:11

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)
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);
            }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!