Set the best Zoom level for your Maps

Android Maps V2 API makes it easy to work with Maps & Added POI (Marker), but what if you have a lot of Markers to display?

Two possibilities comes in hand:

  1. Use this wonderful Map extensions lib, that extends Maps V2 to regroup nicely the stack of markers.
  2. Find the appropriate zoom level that is not overwhelming for the user and display a restricted number of POI around a location.
    This is actually what I implemented for a professional project
  • On the left image is what we’re trying to avoid (Markers are stacked).
  • On the right image is the desired effect, here markers are placed proportionally because we set the perfect zoom level that allow us to display just 3 POI within a radius of 1 kilometer.

The idea to achieve this is pretty simple:

  1. First We set the zoom level to maximum (ground level)
  2. mSupportFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(;
    mMap = mSupportFrag.getMap();
    MAP_ZOOM_MAX = mMap.getMaxZoomLevel();
    MAP_ZOOM_MIN = mMap.getMinZoomLevel();
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, MAP_ZOOM_MAX));
  3. We use LatLngBounds to get the current rectangle of the Map visible to the user to try to find all available POI within this current Zoom
  4. LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
    for (Marker k : mListMarkers) {
       if (bounds.contains(k.getPosition())) {
  5. If we reach the number of POIs we stop.
  6. keepSearchingForWithinRadius = (Math.round(location.distanceTo(latlngToLocation(bounds.northeast)) / 1000) > radius) ? false : true;
    if (keepSearchingForWithinRadius) {
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, currentZoomLevel--));
  7. Else we keep zooming out until we reach the desired radius
  8. //keep looking but within the limits (we don't want to go outer space do we ?
    if (currentZoomLevel < MAP_ZOOM_MIN) {

More details and code are available on GitHub, you’re free to Hack it 🙂


About nhachicha

Android Developer, share experience about Java, System and Android
This entry was posted in Android and tagged , , , , . Bookmark the permalink.

One Response to Set the best Zoom level for your Maps

  1. Bojan says:

    Hey good simple elegant idea 🙂
    Used in my project.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s