在安卓设备上将谷歌地图缩放到特定英里半径的方法

4
我想在Android中根据我的条件将Google地图缩放到特定半径,例如10英里/20英里/30英里等。我需要做的是从当前的经纬度点绘制一个特定半径(10/20/30英里...),并将地图缩放到该特定半径,以便我可以画出半径圆。我已经能够标记我的当前位置并绘制圆圈。但是我无法将地图缩放到所需的半径英里(圆应该聚焦在屏幕上,以指定的英里数为中心,以我的当前位置为中心)。目前,我正在使用以下代码进行缩放:
gooMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(selectedLat, selectedLong), 15));
                    gooMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
                    Log.e("Circle Lat Long:", selectedLat + ", " + selectedLong);
                    circle.remove();
                    circle = gooMap.addCircle(new CircleOptions()
                            .center(new LatLng(selectedLat, selectedLong))
                            .radius(iMiles * 1609.34) // Converting Miles into Meters...
                            .strokeColor(Color.RED)
                            .strokeWidth(5));
                    circle.isVisible();

我知道将缩放级别设置为15不会将地图缩放到想要的英里数。但我需要将地图缩放到想要的英里半径。我该如何做到?有人能帮我吗? 编辑:添加的图片详细说明了我所需的内容。 Image_1:当我将半径设置为10英里时,地图应该缩放如图所示。 Image_2:当我将半径设置为50英里时,地图应该缩放如图所示。 请查看地图位置差异以分析我需要的缩放级别。 Image_1 Image_2 先感谢您的帮助。

“Zoom to desire(sic) miles radius”是什么意思?“Zoom”是一个无量纲单位。您是否意味着您想让圆圈填满屏幕,或者占屏幕高度的50%?就目前而言,这个问题毫无意义。 - NickT
@NickT 我需要当我缩放屏幕时,屏幕上只显示圆形。 - Pravinsingh Waghela
我再问一遍 - 你想要多大的圆?填满屏幕高度?填满屏幕宽度?占据屏幕的一半? - NickT
@NickT 我正在绘制半径为某些指定英里的圆,比如10英里。现在我想要当地图缩放时,它应该只在屏幕上显示圆的边缘或周长。如果我将英里数增加到40英里,那么圆将再次以40英里半径绘制,我需要地图缩放到再次仅显示我的圆边缘的范围内。不多不少。 - Pravinsingh Waghela
@NickT 我已经编辑了问题并添加了更多细节,请看一下。 - Pravinsingh Waghela
2个回答

7

我有相同问题的解决方案,以下是其中一个:

// Zoom in, animating the camera.
                double iMeter = iMiles * 1609.34;
                circle.remove();
                circle = gooMap.addCircle(new CircleOptions()
                        .center(new LatLng(selectedLat, selectedLong))
                        .radius(iMeter) // Converting Miles into Meters...
                        .strokeColor(Color.RED)
                        .strokeWidth(5));
                circle.isVisible();
                float currentZoomLevel = getZoomLevel(circle);
                float animateZomm = currentZoomLevel + 5;

                Log.e("Zoom Level:", currentZoomLevel + "");
                Log.e("Zoom Level Animate:", animateZomm + "");

                gooMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(selectedLat, selectedLong), animateZomm));
                gooMap.animateCamera(CameraUpdateFactory.zoomTo(currentZoomLevel), 2000, null);
                Log.e("Circle Lat Long:", selectedLat + ", " + selectedLong);

我们根据设备计算缩放级别的方法如下所示:
public float getZoomLevel(Circle circle) {
        float zoomLevel=0;
        if (circle != null){
            double radius = circle.getRadius();
            double scale = radius / 500;
            zoomLevel =(int) (16 - Math.log(scale) / Math.log(2));
        }
        return zoomLevel +.5f;
    }

2
为什么你只是将半径除以500?缩放级别取决于屏幕大小、dpi,因此在不同设备上会显示不同的结果。 - CoolMind
我尝试了这段代码,但结果不正确。一个圆形变得比屏幕还要大。这段代码来自 https://dev59.com/I2025IYBdhLWcg3wjGtO#13159839。 - CoolMind
不适用于不同的屏幕尺寸。例如,对于10英寸的平板电脑,7英寸的平板电脑无法正常工作。 - wonderingdev
不适用于不同屏幕尺寸。例如,对于10英寸的平板电脑,7英寸的平板电脑无法正常工作。 - undefined
@wonderingdev 我们正在将半径除以500的比例值进行调整。你可以在那里适配不同大小的屏幕。尽量使其具有动态性,而不是固定为500。 - Pravinsingh Waghela

2

当视图刷新时,您需要找到屏幕上的距离。虽然我对新的API和视图不太熟悉,但以下内容应该可以解决问题:

  LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
        double llNeLat = bounds.northeast.latitude;
        double llSwLat = bounds.southwest.latitude;
        double llNeLng = bounds.northeast.longitude;
        double llSwLng = bounds.southwest.longitude;
        float results[] = new float[5];
        Location.distanceBetween(llNeLat, llNeLng, llSwLat, llSwLng, results);
        float radius = results[0];
        // radius is distance top right to bottom left on screen in metres
        // so use maybe 3/4 of this, then conert your miles to that value
        // in metres

然后将该值添加到addCircle方法的调用中


请问您能否完成您的解释? - Claude Hangui

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接