7

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?

Bersh
  • 2,789
  • 3
  • 33
  • 47

2 Answers2

4

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.

Bersh
  • 2,789
  • 3
  • 33
  • 47
  • I'm trying out your project now but just a thought - you may want to remove the API key from your manifest file as it probably links back to your google account... – DiscDev Feb 06 '13 at 16:35
  • Nice job! The demo works well for me. I'll try integrating it into one of my existing apps with thousands of markers and see how well it fares with clustering :) Just one question for you - the "interval" you're passing when you clusterize...what units are those in? km? – DiscDev Feb 06 '13 at 16:53
  • "interval" is needed distance between markers on screen in pixels – Bersh Feb 06 '13 at 17:01
  • I tried it out in my app that has about 3,000 markers to plot and it works fairly well. It's a little slow (using a quad core Nexus 4), but I found that increasing the "interval" from 25 to 100 helps speed things up quite a bit. Not sure how it will fare on an old phone. Now what would be really nice is if the icon for the cluster specified how many items are clustered. I know it has it on the overlay when you tap it, but something like the cluster icons here: http://revolver.be/blog/mapkit-clustering-with-ios/ would be awesome. – DiscDev Feb 06 '13 at 17:29
  • Yes cluster images with items count would be realy nice feature. I think to do this like instagram do. But now I've no idea how to implement it, and I've no enought time to investigate it. Maybe I'll back to it when I'll have free time. Some performance improvements also possible :-) – Bersh Feb 07 '13 at 07:48
3

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.

Community
  • 1
  • 1
DiscDev
  • 38,652
  • 20
  • 117
  • 133
  • 1
    Thanks alot for your answer! Looks like I really have to develop my own algorithm. Maybe I'll share my solution later – Bersh Feb 01 '13 at 09:03
  • I'd be very pleased to see what you come up with. Implementing a clustering algorithm for Android has been something that I've wanted to do for a long while, just haven't found the time. I have a feeling it will be a lot easier with the new v2 of the api than the old one. Good luck! – DiscDev Feb 01 '13 at 12:54