Android:如何将地图视图的缩放级别设置为当前位置周围 1 公里半径? [英] Android: How do I set the zoom level of map view to 1 km radius around my current location?

查看:29
本文介绍了Android:如何将地图视图的缩放级别设置为当前位置周围 1 公里半径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将地图视图设置为半径 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?

更新:
在阅读了几篇博文后,我编写了以下代码:

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.

但我不认为我得到的缩放级别显示了 2Km 半径的地图.有人能告诉我我在这里做错了什么吗?

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?

推荐答案

以下代码是最终使用的.考虑到屏幕宽度以及在缩放级别 1 时地球赤道长 256 像素的事实,并且每个后续缩放级别都将表示地球赤道所需的像素数加倍,以下函数返回屏幕将显示区域的缩放级别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;
}

这篇关于Android:如何将地图视图的缩放级别设置为当前位置周围 1 公里半径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆