calculating Lat and Long from Bearing and Distance

后端 未结 2 1187
后悔当初
后悔当初 2020-12-05 07:37

I\'m having a hard time wrapping my head around some Trigonometry. I am trying to deduce a destination latitude and longitude from a start lat and log and distance and bear

2条回答
  •  忘掉有多难
    2020-12-05 07:57

    if anyone needs a function that calculates point coordinates from other point moved by some distance, below is the working code. For me, it's just moving a point by some distance.

    import static java.lang.Math.*;
    
    void movePoint(double latitude, double longitude, double distanceInMetres, double bearing) {
        double brngRad = toRadians(bearing);
        double latRad = toRadians(latitude);
        double lonRad = toRadians(longitude);
        int earthRadiusInMetres = 6371000;
        double distFrac = distanceInMetres / earthRadiusInMetres;
    
        double latitudeResult = asin(sin(latRad) * cos(distFrac) + cos(latRad) * sin(distFrac) * cos(brngRad));
        double a = atan2(sin(brngRad) * sin(distFrac) * cos(latRad), cos(distFrac) - sin(latRad) * sin(latitudeResult));
        double longitudeResult = (lonRad + a + 3 * PI) % (2 * PI) - PI;
    
        System.out.println("latitude: " + toDegrees(latitudeResult) + ", longitude: " + toDegrees(longitudeResult));
    }
    
    • latitude, longitude - entry point coordinates
    • distanceInMetres - distance that you want to move the point by
    • bearing - an angle, direction towards which you want to move the point. 0 is towards the North, 90 - East, 180 - South, 270 - West. And all between, i.e. 45 is North East.
    • earthRadiusInMetres - Earth radius in metres.

    You can change the radius to 6371 if you want to have the input in kilometres or to miles if you want to have input in miles.

提交回复
热议问题