How to get the LatLngBounds of a Circle in Google maps given its center and radius?

♀尐吖头ヾ 提交于 2019-12-01 17:42:39

You can use the SphericalUtil.computeOffset method from the Google Maps Android API Utility Library. To use it you need to the following dependency to your build.gradle:

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

Then, you can calculate the northeast and southwest coordinates of your circle doing:

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);

Essentially copying and pasting antonio's answer:

    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;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!