Android: How do I set the zoom level of map view to 1 km radius around my current location?

前端 未结 8 1732
日久生厌
日久生厌 2020-11-28 03:01

I want to set the map view zoomed to 1km radius but cant figure out how?

The doc says that the zoom level 1 will map earths equator to 256 pixels. So how do I calcul

8条回答
  •  情深已故
    2020-11-28 03:39

    I've converted the accepted answer to return a double value, since the Android Google Maps library uses floating point zoom levels, and also account for latitudes away from the equator.

    public static double getZoomForMetersWide (
      final double desiredMeters,
      final double mapWidth,
      final double latitude )
    {
      final double latitudinalAdjustment = Math.cos( Math.PI * latitude / 180.0 );
    
      final double arg = EQUATOR_LENGTH * mapWidth * latitudinalAdjustment / ( desiredMeters * 256.0 );
    
      return Math.log( arg ) / Math.log( 2.0 );
    }
    

    As an aside, for best results on Android don't pass the view's real pixel count, but the dimension scaled for the device's pixel density.

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    float mapWidth = mapView.getWidth() / metrics.scaledDensity;
    

    Hope this helps someone.

提交回复
热议问题