如何根据Google地图上圆形的中心点和半径获取其LatLngBounds?

7

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


这个答案中有一个只使用简单数学的解决方案。 - Thomas Fritsch
2个回答

10

您可以使用Google Maps Android API Utility Library中的SphericalUtil.computeOffset方法。要使用它,您需要将以下依赖项添加到您的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);

45和225作为标题代表什么意思? - M Umer
2
45度是朝东北方向(0度表示北),225度是朝西南方向(180度即南加上45度)。 - antonio

4

基本上是复制并粘贴Antonio的答案:

    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就是这个距离。看起来这是从中心获取角落距离的一种方法。很棒。 - j2emanue
你是对的。乘以sqrt(2)是为了填充。这不是必须的,半径就足够了。 - Maxime Claude

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