安卓:我如何设置地图视图缩放级别为1公里的半径在我当前的位置?在我、缩放、半径、视图

2023-09-12 07:08:58 作者:怀里的折耳猫

我想设置缩放到1公里半径地图视图,但无法弄清楚如何?

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

该医生说,该缩放级别1将地球赤道上空映射到256像素。那么,如何计算我需要设置使地图视图1公里半径的区域显示其缩放比例?

The doc says that the zoom level 1 will map earths equator to 256 pixels. So how do I calculate which zoom level I need to set so that the map view shows area in 1KM radius?

更新: 阅读一些博客文章后,我写了下面的code:

UPDATE: After reading a few blog posts I wrote the following code:

private int calculateZoomLevel() {
    double equatorLength = 6378140; // 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;
}

我们的想法是每个像素在缩放级别1,而根据谷歌给出了使用256像素地球赤道,首先我计算出的米。现在,以后每缩放级别放大由2让我半每个像素的米每缩放级别的水平。我这样做,直到我有一个缩放级别,其中每像素米乘以在屏幕宽度给我不到2000即2公里跨越。

The idea is that first I calculate Meters per pixel in the zoom level 1, which according to google shows equator of earth using 256 pixels. Now every subsequent zoom level magnifies by a level of 2 so I half the meters per pixel for every zoom level. I do this until I have a zoom level where meters per pixel multiplied by the screen width gives me less than 2000 i.e 2 Km across.

但我不认为缩放级别我得到是显示2公里半径的地图。有些人能告诉我什么,我做错了什么?

But I dont think that the zoom level I am getting is showing the map of 2Km radius. Can some one tell me what I am doing wrong here?

推荐答案

以下code是什么最终使用。给出的屏幕宽度和事实,即在缩放级别1地球赤道是256像素长和每个后续缩放级别双打重新present稀土赤道所需的像素数,以下函数返回缩放级别时的屏幕将显示2公里宽度的区域。

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;
}