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

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

    The following code is what ended up using. Given the screen width and the fact that at zoom level 1 the equator of Earth is 256 pixels long and every subsequent zoom level doubles the number of pixels needed to represent earths equator, the following function returns the zoom level where the screen will show an area of 2Km width.

    private int calculateZoomLevel(int screenWidth) {
        double equatorLength = 40075004; // in meters
        double widthInPixels = screenWidth;
        double metersPerPixel = equatorLength / 256;
        int zoomLevel = 1;
        while ((metersPerPixel * widthInPixels) > 2000) {
            metersPerPixel /= 2;
            ++zoomLevel;
        }
        Log.i("ADNAN", "zoom level = "+zoomLevel);
        return zoomLevel;
    }
    

提交回复
热议问题