【问题标题】:Android alternative to iOS Google Maps API GMSGeometryOffsetAndroid 替代 iOS Google Maps API GMSGeometryOffset
【发布时间】:2015-05-22 12:59:23
【问题描述】:

我正在开发我开发的 Android 版本的 Ios 应用程序,但在两个平台上的 Google Maps API 存在差异时遇到了问题。在 Ios 中我可以使用方法:GMSGeometryOffset。但是,它不存在于 Android API 中。因此,我正在寻找这种方法的替代方法,以便我可以根据当前位置、方向/方位和距离/半径来计算新位置。

我用它来画一个圆,同时避开蛋形。到目前为止,我的代码如下所示。但是,除非当前位置直接在赤道之上,否则它会给我一个蛋形。

for(double angle = 0; angle < 2*Math.PI; angle = angle+ Math.PI/resolutionCircle)
        {
            latitude = position.latitude + (radiusCircle * Math.sin(angle));
            longitude = position.longitude + (radiusCircle * Math.cos(angle));

            inner.add(new LatLng(latitude, longitude));
        }

【问题讨论】:

    标签: android ios google-maps


    【解决方案1】:

    Google Maps Android API 实用程序库中有一个computeOffset 方法。

    这是一个静态方法:

    public static LatLng computeOffset(LatLng from,
                       double distance,
                       double heading)
    

    它返回从指定航向中的原点移动一段距离所产生的 La​​tLng(以从北顺时针方向的度数表示)。

    您可以参考this documentation 了解有关 Google Maps Android API 实用程序库的更多详细信息。

    谷歌地图的GitHub page也提供了它的详细实现:

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

    【讨论】:

    • 这个距离应该有多大?以米或公里为单位。如果我想考虑地图中心的角落?
    猜你喜欢
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 2015-04-06
    • 2023-04-02
    相关资源
    最近更新 更多