Calculate bearing between two locations (lat, long)

后端 未结 7 1549
不知归路
不知归路 2020-12-13 20:35

I\'m trying to develop my own augmented reality engine.

Searching on internet, I\'ve found this useful tutorial. Reading it I see that the important thing is bearing

7条回答
  •  醉梦人生
    2020-12-13 20:51

    Try this for accurate result:

    private static double degreeToRadians(double latLong) {
        return (Math.PI * latLong / 180.0);
    }
    
    private static double radiansToDegree(double latLong) {
        return (latLong * 180.0 / Math.PI);
    }
    
    public static double getBearing() {
    
    //Source
    JSONObject source = step.getJSONObject("start_location");
    double lat1 = Double.parseDouble(source.getString("lat"));
    double lng1 = Double.parseDouble(source.getString("lng"));
    
    // destination
    JSONObject destination = step.getJSONObject("end_location");
    double lat2 = Double.parseDouble(destination.getString("lat"));
    double lng2 = Double.parseDouble(destination.getString("lng"));
    
        double fLat = degreeToRadians(lat1);
        double fLong = degreeToRadians(lng1);
        double tLat = degreeToRadians(lat2);
        double tLong = degreeToRadians(lng2);
    
        double dLon = (tLong - fLong);
    
        double degree = radiansToDegree(Math.atan2(sin(dLon) * cos(tLat),
                cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(dLon)));
    
        if (degree >= 0) {
            return degree;
        } else {
            return 360 + degree;
        }
    }
    

    You can test bearing result on http://www.sunearthtools.com/tools/distance.php .

提交回复
热议问题