Calculate bearing between two locations (lat, long)

后端 未结 7 1685
不知归路
不知归路 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:54

    Calculate bearing

    //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 dLon = (lng2-lng1);
    double y = Math.sin(dLon) * Math.cos(lat2);
    double x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
    double brng = Math.toDegrees((Math.atan2(y, x)));
    brng = (360 - ((brng + 360) % 360));
    

    Convert Degrees into Radians

    Radians = Degrees * PI / 180
    

    Convert Radians into Degrees

    Degrees = Radians * 180 / PI
    

提交回复
热议问题