Show or hide markers depends on zoom level

I draw a lot of markers on the map, and when they are close, they overlap each other. Therefore, I want to hide some markers with a little scaling and show more markers when scaling a user's map. Like more magnification, more marks. Here is an example of activity and marker creation code, as you can see that I am using google maps android api v2:

public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); map.getUiSettings().setMyLocationButtonEnabled(true); createMarkers(map); } private void createMarkers(GoogleMap map) { double initLat = 48.462740; double initLng = 35.039572; for(float i = 0; i < 2; i+=0.2) { LatLng pos = new LatLng(initLat + i,initLng); map.addMarker(new MarkerOptions() .position(pos) .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))); } for(float i = 0; i < 2; i+=0.2) { LatLng pos = new LatLng(initLat, initLng + i); map.addMarker(new MarkerOptions() .position(pos) .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))); } } 

This is a typical task for me, but I still could not find a working solution. I read this article https://developers.google.com/maps/articles/toomanymarkers , but I do not know how to implement it on Android. Does anyone have working code that can do this?

+7
source share
2 answers

Here is my solution to this problem: https://github.com/Bersh/MarkersCluster

This may not be the best solution, but it works for me. Hope this will be helpful.

+4
source

Basically you ask how to implement clustering on Android. To my knowledge, Google does not offer a solution for this. The document you refer to in the comments ( developers.google.com/maps/articles/toomanymarkers ) refers to the google maps JavaScript API. Unfortunately, none of the cluster-related codes are yet available on Android.

You will have to come up with your own marker clustering algorithm. The developers.google.com/maps/articles/toomanymarkers document you pointed out can be a good starting point to determine which algorithm will work best for you (grid-based clustering, distance-based clustering, etc. )

Another option, which is slightly easier to implement (but not perfect), may be to change the marker image to a smaller icon when the zoom level has changed, so they do not overlap. Have a look at my answer to this question: https://stackoverflow.com/a/316618/

You can use this answer to determine when you crossed the “zoom threshold” and change the markers to something smaller once you zoom in on a certain threshold and zoom in when you zoom in. I use this trick in several of my applications to change the usual icons to small dot images when zooming out and return to the regular icon when zooming in again.

+3
source

All Articles