给定位置精度,为Google Maps确定合理的缩放级别 [英] Determine a reasonable zoom level for Google Maps given location accuracy

查看:48
本文介绍了给定位置精度,为Google Maps确定合理的缩放级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Google Map定位到用户位置,同时考虑到该位置的准确性,给出合理的缩放级别.有人可以描述我应该如何计算吗?涉及哪些变量,如何实现?

I am trying to center a Google Map to the user location while giving a reasonable zoom level taking into account the accuracy of that location. Could anybody describe how should I compute it? Which variables are involved, how do you achieve this?

推荐答案

您正在寻找的公式是根据位置的准确度来计算缩放级别的.

What you are looking for is the formula that calculates the zoom level based on the accuracy of the location.

我设法提出了这个公式(在我的测试中),效果很好.

I managed to come up with this formula which (in my tests) worked pretty well.

这可以简化为(可能并非如此):

This can be simplified (might not seem so) to this:

这恐怖的东西就是你想要的.

This scary looking thing is what you want.

EquatorLength 是40,075,004米.通过将精度圆的直径除以设备屏幕的长度(以像素为单位),可以计算出 Meters/Pixel .

EquatorLength is 40,075,004 meters. While the Meters/Pixel can be calculated by diving the diameter of the accuracy circle by the length of the device screen (in pixels).

这是我用来测试该公式的示例程序:

Here's a sample program that I used to test this formula:

GoogleMap mMap;

@Override
protected void onStart() {
    super.onStart();

    mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

    // Enable user's location layer
    mMap.setMyLocationEnabled(true);

    mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            // Location lat-lng
            LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());

            // Location accuracy diameter (in meters)
            float accuracy = location.getAccuracy() * 2;

            // Screen measurements
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            // Use min(width, height) (to properly fit the screen
            int screenSize = Math.min(metrics.widthPixels, metrics.heightPixels);

            // Equators length
            long equator = 40075004;

            // The meters per pixel required to show the whole area the user might be located in
            double requiredMpp = accuracy/screenSize;

            // Calculate the zoom level
            double zoomLevel = ((Math.log(equator / (256 * requiredMpp))) / Math.log(2)) + 1;

            Log.e(TAG, String.format("Accuracy: %f. Screen Width: %d, Height: %d",
                    accuracy, metrics.widthPixels, metrics.heightPixels));
            Log.e(TAG, String.format("Required M/Px: %f Zoom Level: %f Approx Zoom Level: %d",
                    requiredMpp, zoomLevel, calculateZoomLevel(screenSize, accuracy)));

            // Center to user's position
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, (float) zoomLevel));

            // Prevent the camera centering on the user again
            mMap.setOnMyLocationChangeListener(null);
        }
    });

}

private int calculateZoomLevel(int screenWidth, float accuracy) {
    double equatorLength = 40075004; // in meters
    double metersPerPixel = equatorLength / 256;
    int zoomLevel = 1;
    while ((metersPerPixel * (double) screenWidth) > accuracy) {
        metersPerPixel /= 2;
        zoomLevel++;
    }

    return zoomLevel;
}

几件事要注意:

  • 此答案基于,并将其实现以检查生成的值
  • 准确度是用户位置的半径,根据文档,可以准确度高达 68%.
  • This answer is based on this and implements it to check the values generated
  • Accuracy is the radius of user's location and according to the docs it can be up to 68% correct.

非常欢迎任何更正.

这篇关于给定位置精度,为Google Maps确定合理的缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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