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

前端 未结 8 1729
日久生厌
日久生厌 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:55

    FINAL working solution:

    public static void getZoomForMetersWide(GoogleMap googleMap, int mapViewWidth, LatLng latLngPoint, int desiredMeters) {
            DisplayMetrics metrics = App.getAppCtx().getResources().getDisplayMetrics();
            float mapWidth = mapViewWidth / metrics.density;
    
            final int EQUATOR_LENGTH = 40075004;
            final int TIME_ANIMATION_MILIS = 1500;
            final double latitudinalAdjustment = Math.cos(Math.PI * latLngPoint.latitude / 180.0);
            final double arg = EQUATOR_LENGTH * mapWidth * latitudinalAdjustment / (desiredMeters * 256.0);
            double valToZoom = Math.log(arg) / Math.log(2.0);
    
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLngPoint, Float.valueOf(String.valueOf(valToZoom))), TIME_ANIMATION_MILIS , null);
        }
    

    p.s. Using @sho answer and @Lionel Briand comment

    0 讨论(0)
  • 2020-11-28 04:00

    although this answer is logical and i find it working but the results are not accurate i dont know why but i tired this approach and this technique is far more accurate.

    1) Make a circle on object with desired radius

    Circle circle = mGoogleMap.addCircle(new CircleOptions().center(new LatLng(latitude, longitude)).radius(getRadiusInMeters()).strokeColor(Color.RED));           
            circle.setVisible(true);
            getZoomLevel(circle);
    

    2) Pass that object to this function and set the zoom level Here's a link

    public int getZoomLevel(Circle circle) {
    if (circle != null){
        double radius = circle.getRadius();
        double scale = radius / 500;
        zoomLevel =(int) (16 - Math.log(scale) / Math.log(2));
    }
    return zoomLevel;
    }
    
    0 讨论(0)
提交回复
热议问题