【问题标题】:Google Places API LatLngBounds from LatLng and Radius来自 LatLng 和 Radius 的 Google Places API LatLngBounds
【发布时间】:2015-06-15 13:15:00
【问题描述】:

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

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

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

谢谢。

【问题讨论】:

  • LatLngBounds 是方形边界框,而不是圆形 - 您想如何插入圆形和方形?
  • 类似于“spherical.computeOffset”的东西。我最近做了这个,用 latlng 创建一个视口。让我知道这是否是您的想法。
  • @luke_mclachlan 是的,但在 java 中。我正在努力记住我的三角学课程并建立起来。
  • 我有一个 javascript 代码,我会将其作为答案发布,以防万一它对您有用。这是完整的工作代码。

标签: android google-places-api


【解决方案1】:

正如 Distwo 所承诺的,这就是我如何基于单个纬度和经度创建视口。我想要这样做的原因是我的商店定位器脚本从 Places API 返回的视口中搜索,因此如果没有返回视口(很少见,但确实发生了),我会继续从 lat 和 lng 创建一个由地方返回。请注意,我没有使用 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”。

【讨论】:

  • 感谢您,我找到了一个具有 computeOffset 的 java github!我会在下面发布。
【解决方案2】:

感谢@luke_mclachlan,我在google map 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 对象。

【讨论】:

  • 距离以米为单位?
  • 是的,距离以米为单位。
  • @Distwo 如果我想覆盖屏幕的边框,该距离应该有多长?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多