当用户键入时,我正在使用新的Android位置Api来获得自动完成的预测。
据我所见,API获取了一个LatLngBounds对象,该对象是使用两个位置创建的。
有没有一种方法可以使用一个LatLng作为中心点和半径来生成LatLngBounds对象?
谢谢。
正如Distwo承诺的那样,以下是我如何基于单个纬度和经度创建视口。我想这样做的原因是,我的商店定位器脚本从Places API返回的视口中进行搜索,因此,如果没有返回视口(很罕见,但确实发生了),我会继续从Places返回的lat和lng中创建一个。请注意,我没有使用安卓应用程序,这是为了定期查看网页。
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,它代表了一个lilometer半径。如果你使用公里数,那么1000是要使用的数字,但如果你使用英里数,那么请使用"1609.3440006"。
感谢@luke_mclachlan,我在谷歌地图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对象。