【问题标题】:How to get the LatLngBounds of a Circle in Google maps given its center and radius?给定圆心和半径,如何在 Google 地图中获取圆的 LatLngBounds?
【发布时间】:2016-05-18 15:24:12
【问题描述】:

如何获得具有给定中心和半径的圆的东北和西南坐标?我在任何地方都找不到任何解决方案。目前,与以前不同,Circle 对象没有 getLatLngBounds() 和 getBounds() 方法。

【问题讨论】:

  • this answer 中有一个只使用简单数学的解决方案。

标签: android google-maps android-studio coordinates geometry


【解决方案1】:

您可以使用SphericalUtil.computeOffset 方法 Google Maps Android API Utility Library。要使用它,您需要对 build.gradle 的以下依赖项:

dependencies {
    compile 'com.google.maps.android:android-maps-utils:0.4+'
}

然后,您可以计算圆的东北和西南坐标:

double radius = 104.52;
LatLng center = new LatLng(40.22861, -3.95567);

LatLng targetNorthEast = SphericalUtil.computeOffset(center, radius * Math.sqrt(2), 45);
LatLng targetSouthWest = SphericalUtil.computeOffset(center, radius * Math.sqrt(2), 225);

【讨论】:

    【解决方案2】:

    基本上是复制和粘贴安东尼奥的答案:

        public static LatLngBounds getLatLngBoundsFromCircle(Circle circle){
            if(circle != null){
                return new LatLngBounds.Builder()
                        .include(SphericalUtil.computeOffset(circle.getCenter(), circle.getRadius() * Math.sqrt(2), 45))
                        .include(SphericalUtil.computeOffset(circle.getCenter(), circle.getRadius() * Math.sqrt(2), 225))
                        .build();
            }
            return null;
        }
    

    【讨论】:

    • 嘿,你能告诉我为什么要乘 Math.sqrt(2)。为什么不直接使用半径作为距离呢?我认为 distanceFromCenterToCorner 是正确的。看起来它是一种从中心获得角距离的方法。很酷。
    • 你是对的。 sqrt(2) 的乘法用于填充。不是强制的,半径就够了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 2014-11-09
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    相关资源
    最近更新 更多