Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show or hide markers depends on zoom level

I'm drawing a lot of markers on the map and when they located close they overlap each other. So I want to hide some markers on small zoom and show more markers when user zooming map. Like more zoom in, more markes. Here is example code of activity and creating of markers, as you can see I'm 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)));
    }
}

It sounds like typical task for me, but i still didn't manage to find working solution. I've read this article https://developers.google.com/maps/articles/toomanymarkers but I've no idea how to implement it on android. Does anybody has some working code which can do this?

like image 590
Bersh Avatar asked Jan 31 '13 08:01

Bersh


2 Answers

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

Maybe it's not the best solution, but it works for me. Hope it'll be usefull.

like image 52
Bersh Avatar answered Oct 23 '22 12:10

Bersh


You are basically asking how to implement clustering on Android. As far as I know, there is no solution provided by Google to do this. The document you referenced in the comments (developers.google.com/maps/articles/toomanymarkers) is referring to the google maps javascript API. Unfortunately, none of the clustering related code is yet available on Android.

You will have to come up with your own algorithm to cluster Markers. The developers.google.com/maps/articles/toomanymarkers document that you pointed to may be a good starting point to decide what algorithm will work best for you (grid based clustering, distance based clustering, etc).

Another option that's slightly easier to implement (but not perfect) might be to change the Marker image to a smaller icon when the zoom level has changed so they don't overlap as much. Check out my answer to this question: https://stackoverflow.com/a/13976080/1103584

You could use that answer to determine when you've crossed a "zoom threshold" and change the Markers to something smaller once you've zoomed out passed a certain threshold and to a bigger image when you've zoomed back in. I use this trick in a couple of my own apps to change regular icons to small dot images when zoomed out, and back to the regular icon when you zoom back in.

like image 33
DiscDev Avatar answered Oct 23 '22 10:10

DiscDev