How to change 1 meter to pixel distance?

后端 未结 5 2144
一个人的身影
一个人的身影 2020-12-25 09:50

When I develop an Android map application, I want to draw a circle on the map whose radius is 1 meter. As you known, I can\'t draw 1 meter directly, I should convert 1 meter

5条回答
  •  时光说笑
    2020-12-25 10:20

    You can calculate the zoom level for the radius you want:

    First we need to calculate the screen width of the phone.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 1Km width.

    private int calculateZoomLevel() {
    
        int ht, screenWidth;
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        ht = displaymetrics.heightPixels;
        screenWidth = displaymetrics.widthPixels;
    
        double equatorLength = 40075004; // in meters
        double widthInPixels = screenWidth;
        double metersPerPixel = equatorLength / 256;
        int zoomLevel = 1;
        while ((metersPerPixel * widthInPixels) > 1000) {
            metersPerPixel /= 2;
            ++zoomLevel;
        }
    
        Log.i(tag, "zoom level = " + zoomLevel);
        return zoomLevel;
    }
    

提交回复
热议问题