Google 从 LatLng 和 Radius 放置 API LatLngBounds

Google Places API LatLngBounds from LatLng and Radius

我正在使用新的 Android 地方 Api 在用户键入时获得自动完成预测。

据我目前所见,API 采用了一个使用两个位置创建的 LatLngBounds 对象。

有没有一种方法可以使用一个 LatLng 作为中心点和半径来生成 LatLngBounds 对象?

谢谢。

正如 Distwo 所承诺的,下面是我如何基于单个纬度和经度创建视口。我想要这样做的原因是我的商店定位器脚本从 Places API 返回的视口进行搜索,因此如果未返回视口(很少见但会发生)我继续创建一个地点返回的纬度和经度。请注意,我没有使用 android 应用程序,这是用于常规网络查看。

console.log("no viewport found so lets spherically compute the bounds");
var sw = google.maps.geometry.spherical.computeOffset(place.geometry.location, 1609, 225);
var ne = google.maps.geometry.spherical.computeOffset(place.geometry.location, 1609, 45);
swlat = sw.lat().toFixed(6);
swlng = sw.lng().toFixed(6);
nelat = ne.lat().toFixed(6);
nelng = ne.lng().toFixed(6);

不确定这对您有用。在我的例子中,半径是固定的,但在你的情况下,它听起来虽然是一个变量,因此你必须将它作为一个变量来引用。

好的,稍微编辑一下:我已将 1423 更改为 1000,代表一米半径。如果您使用公里,那么 1000 是要使用的数字,但如果您使用英里,则使用“1609.3440006”代替。

感谢@luke_mclachlan,我在google地图上找到了我要找的方法github。

https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/SphericalUtil.java

这是我一直在寻找的方法:

public static LatLng computeOffset(LatLng from, double distance, double heading) {
        distance /= EARTH_RADIUS;
        heading = toRadians(heading);
        // http://williams.best.vwh.net/avform.htm#LL
        double fromLat = toRadians(from.latitude);
        double fromLng = toRadians(from.longitude);
        double cosDistance = cos(distance);
        double sinDistance = sin(distance);
        double sinFromLat = sin(fromLat);
        double cosFromLat = cos(fromLat);
        double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * cos(heading);
        double dLng = atan2(
                sinDistance * cosFromLat * sin(heading),
                cosDistance - sinFromLat * sinLat);
        return new LatLng(toDegrees(asin(sinLat)), toDegrees(fromLng + dLng));
    }

请注意,此处的心脏半径预计以米为单位。

使用此方法,我找到西南(航向 = 225)和东北(航向 = 45)坐标,然后创建我的 LatLongBounds 对象。