安卓如何从当前用户位置查找朝向目的地的角度(0->360)



我具有用户和目的地的当前经度和纬度(只是静态位置)。如何在当前用户的位置和目的地之间找到方向angle(0->360)

我目前正在使用此处记录的currentLocation.bearingTo(destination),但我找不到正确的角度。

最后,我为此找到了解决方案,我在这里分享,因为它可以帮助他人。

private double bearing(double startLat, double startLng, double endLat, double endLng){
        double longitude1 = startLng;
        double longitude2 = endLng;
        double latitude1 = Math.toRadians(startLat);
        double latitude2 = Math.toRadians(endLat);
        double longDiff= Math.toRadians(longitude2-longitude1);
        double y= Math.sin(longDiff)*Math.cos(latitude2);
        double x=Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);
        return (Math.toDegrees(Math.atan2(y, x))+360)%360;
    }

如果方法不返回所需的值,则必须检查经度和纬度值。请注意,此方法返回0-> 360度之间的值。

最新更新