Bearing from one coordinate to another

后端 未结 4 1274
北恋
北恋 2020-12-08 21:33

I implemented the \"bearing\" formula from http://www.movable-type.co.uk/scripts/latlong.html. But it seems highly inaccurate - I suspect some mistakes in my implementation.

4条回答
  •  渐次进展
    2020-12-08 21:50

    Bearing from one coordinate to another And Find North,East,south,weast :)enter image description here

         public class FindBearing {
                public static void main(String[] args) {
                    System.out.println(" Your Result >>> "+FindBearing.bearing(19.2859590, 73.4966430, 19.2861020, 73.4988090));    
                }   
                protected static String bearing(double lat1, double lon1, double lat2, double lon2){
              double longitude1 = lon1;
              double longitude2 = lon2;
              double latitude1 = Math.toRadians(lat1);
              double latitude2 = Math.toRadians(lat2);
              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);
              double resultDegree= (Math.toDegrees(Math.atan2(y, x))+360)%360;
              String coordNames[] = {"N","NNE", "NE","ENE","E", "ESE","SE","SSE", "S","SSW", "SW","WSW", "W","WNW", "NW","NNW", "N"};
              double directionid = Math.round(resultDegree / 22.5); 
              // no of array contain 360/16=22.5
              if (directionid < 0) {
                  directionid = directionid + 16;
                   //no. of contains in array
              }
              String compasLoc=coordNames[(int) directionid];
    
              return resultDegree+" "+compasLoc;
            }
                }
    

提交回复
热议问题