Android How to set the best Zoom level for your Maps v2

Public on May 25, 2016
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. 1
      2
      3
      4
      5
      6
      mSupportFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
      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. 1
      2
      3
      4
      5
      6
      7
      LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
      for (Marker k : mListMarkers) {
         if (bounds.contains(k.getPosition())) {
            currentFoundPoi++;
         }
      }
    5. If we reach the number of POIs we stop.
    6. 1
      2
      3
      4
      5
      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. 1
      2
      3
      4
      //keep looking but within the limits (we don't want to go outer space do we ?
      if (currentZoomLevel < MAP_ZOOM_MIN) {
        break;
      }
    More details android developer tutorial and this code are available on GitHub, you’re free to Hack it:)
    Source : https://nhachicha.wordpress.com/2013/06/01/set-the-best-zoom-level-for-your-maps/




    [Advertising]Faster Thinking - Speed up your brain


    Faster Thinking Game



    sentiment_satisfied Emoticon