android - 获取两点(LatLng)与垂直轴(北轴)之间的角度

标签 android vector google-maps-api-2 angle

目前,我在谷歌地图上有两个点(LatLng)A和B。 我将我的问题如下图所示: enter image description here

当垂直轴旋转设备时,我想得到A点和B点之间的角度@ enter image description here

我想获取 AB 和北轴(y 轴)之间的角度 我该怎么办。 我创建了一个函数来获取角度,但它返回 null :(

    public double getAngleTwoPoint(LatLng point1,LatLng point2){
    double lat1=point1.latitude;
    double lat2=point2.latitude;
    double long1=point1.longitude;
    double long2=point2.longitude;
    double deltaLong=long2-long1;
    double angle = Math.atan2(Math.sin(deltaLong)*Math.cos(lat2), Math.cos(lat1)*Math.sin(lat2)-Math.sin(lat1)*Math.cos(lat2)*Math.cos(deltaLong));     
    return Math.toDegrees(angle);
}

最佳答案

您可以使用此公式来获取经纬度之间的角度,看看。

θ = atan2(sin(Δlong)*cos(lat2), cos(lat1)*sin(lat2) − sin(lat1)*cos(lat2)*cos(Δlong))

请注意,在使用此公式之前应将角度(θ)转换为弧度,并且 Δlong = long2 - long1。

atan2 是几乎所有编程语言中都存在的常见函数(主要在 Math 包中)。通常还有度数和弧度之间转换的函数(也在 Math 包中)。

请记住,atan2 返回的值范围为 -π ... +π,要将结果转换为罗盘方位,您需要将 θ 乘以 180/π,然后使用 (θ+360) % 360,其中 %是模除运算,返回除法的余数。

以下链接是涉及纬度和经度的公式的良好资源。他们还提供公式的 Javascript 实现。事实上,答案是基于此页面的信息:

http://www.yourhomenow.com/house/haversine.html

关于android - 获取两点(LatLng)与垂直轴(北轴)之间的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19973070/

相关文章:

java - Android TextView 的 HTML 中的换行符

Android:滚动后 RecyclerView 内容困惑

android - 如何将水平回收 View 居中居中?

java - 在 android 中使用 Google map api v2 显示我的轨迹

ios - 在 iOS 中更改 GMSMapView 默认 map 背景颜色

android - Google Maps Android API v2 - 检测 map 上的触摸

Android - 如何在 Android Recyclerview 中搜索过滤器后获取原始项目位置

c++ - vector 中的未知变量列大小

c++ - vector 大小的段错误

c++ - 如果我声明一个没有名字的类实例,它会留在内存中吗?