【问题标题】:Zooming Google map to specific radius in miles in android在android中将Google地图缩放到以英里为单位的特定半径
【发布时间】:2017-01-16 02:28:42
【问题描述】:

我想 zoom Google map 根据我在 android 中的情况以英里为单位的特定半径,例如 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 英里时,地图应如图所示缩放。 请查看地图位置的差异以分析我需要的缩放级别。 提前致谢。

【问题讨论】:

  • 您所说的“放大到想要的(原文如此)英里半径”是什么意思?缩放是一个无量纲单位。你的意思是你想让圆圈填满屏幕,或者说是屏幕高度的 50%?就目前而言,这个问题毫无意义。
  • @NickT 我需要缩放屏幕时应该只在屏幕上显示圆圈。
  • 我再次问 - 你想要多大的圈子?填充屏幕高度?填充屏幕宽度?占据半个屏幕?
  • @NickT 我正在绘制半径为某些指定英里的圆,例如 10 英里。现在我想在地图缩放时只在屏幕上显示圆形边缘或周长。如果我将英里数增加到 40 英里,则再次以 40 英里的半径绘制圆圈,我再次需要将地图缩放到我的圆圈边缘再次显示在屏幕上的范围。不多也不少。
  • @NickT 我已经用更多细节编辑了这个问题。请看看

标签: android google-maps google-maps-api-3 android-maps-v2 android-maps


【解决方案1】:

我得到了相同的解决方案,这是一个:

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

【讨论】:

  • 你为什么简单地将半径除以 500?缩放级别取决于屏幕尺寸、dpi,因此它会在不同的设备上显示不同的结果。
  • 我试过这段代码。它给出了错误的结果。一个圆圈变得比屏幕还大。此代码来自stackoverflow.com/a/13159839/2914140
【解决方案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 调用

【讨论】:

  • 能否请您完成您的解释?
猜你喜欢
  • 2012-02-25
  • 2011-06-28
  • 1970-01-01
  • 2015-10-12
  • 2017-08-14
  • 2020-12-06
  • 2014-11-09
  • 2018-12-14
  • 2011-08-25
相关资源
最近更新 更多