在没有目的地的谷歌地图上获取 x 公里后的纬度

Get Latitude Longitude after x kilometer on Google Map without destination?(在没有目的地的谷歌地图上获取 x 公里后的纬度经度?)
本文介绍了在没有目的地的谷歌地图上获取 x 公里后的纬度经度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 Android 应用程序,它需要在 X 公里后在同一条路线上找到一个坐标.

I am creating an Android app which requires finding a coordinate on the same route after X kilometers.

我有两个坐标 x1,y1 &x2,y2 在路上.现在,我的要求是在大约 3 公里后找到坐标 x3,y3 (即,在 x2,y2 之后的坐标不在 x1,y1 和; x2,y2) 在同一条路上.

I have two coordinates x1,y1 & x2,y2 on a road. Now, my requirement is to find coordinate x3,y3 after some 3 kilometers (i.e., coordinate after x2,y2 not between x1,y1 & x2,y2) on the same road.

如何做到这一点?

推荐答案

如果你知道 轴承,可以计算出目的地坐标.

If you know the bearing, you can calculate the destination coordinate.

示例代码:

private LatLng getDestinationPoint(LatLng source, double brng, double dist) {
        dist = dist / 6371;
        brng = Math.toRadians(brng);

        double lat1 = Math.toRadians(source.latitude), lon1 = Math.toRadians(source.longitude);
        double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
                                Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
        double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
                                        Math.cos(lat1),
                                        Math.cos(dist) - Math.sin(lat1) *
                                        Math.sin(lat2));
        if (Double.isNaN(lat2) || Double.isNaN(lon2)) {
            return null;
        }
        return new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2));
    }

示例用法:

   double radiusInKM = 10.0;
   double bearing = 90;
   LatLng destinationPoint = getDestinationPoint(new LatLng((25.48, -71.26), bearing, radiusInKM);

或者您可以在 pointA 和 pointB 之间使用航向而不是方位:

Or you can use heading between your pointA and pointB instead of bearing:

LatLng destinationPoint = getDestinationPoint(new LatLng(37.4038194,-122.081267), SphericalUtil.computeHeading(new LatLng(37.7577,-122.4376), new LatLng(37.4038194,-122.081267)), radiusInKM);

SphericalUtil.computeHeading(p1, p2); 方法来自 Android Google 地图实用程序库.

这是基于 this Stackoverflow answer 中的 Javascript 方法.

This is based on the Javascript method from this Stackoverflow answer.

如果您想在同一条道路上找到该点,您可以查看此 PHP 答案.

If you want the point on same road, you might checkout this PHP answer.

这篇关于在没有目的地的谷歌地图上获取 x 公里后的纬度经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)