计算从一条线到点的地理距离

时间:2017-06-22 17:52:12

标签: java android location distance

我在论坛中有这段代码,它用于计算从一条线到一条点的距离。问题是我不知道返回结果的度量单位。这是代码:

private double distanceFromLineToPoint(Point A, Point B, Point C){ //A-B the line, C the point

     double lat1=A.latitude;
     double lon1=A.longitude;
     double lat2=B.latitude;
     double lon2=B.longitude;
     double lat3=C.latitude;
     double lon3=C.longitude;
     double EARTH_RADIUS_KM=6371;

     double y=Math.sin(lon3-lon1)*Math.cos(lat3);
     double x = Math.cos(lat1) * Math.sin(lat3) - Math.sin(lat1) * Math.cos(lat3) * Math.cos(lat3 - lat1);
     double bearing1=Math.toDegrees(Math.atan2(y,x));
     bearing1=360-(bearing1+360%360);

     double y2 = Math.sin(lon2 - lon1) * Math.cos(lat2);
     double x2 = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lat2 - lat1);
     double bearing2 = Math.toDegrees(Math.atan2(y2, x2));
     bearing2 = 360 - (bearing2 + 360 % 360);

     double lat1Rads = Math.toRadians(lat1);
     double lat3Rads = Math.toRadians(lat3);
     double dLon = Math.toRadians(lon3 - lon1);

     double distanceAC = Math.acos(Math.sin(lat1Rads) * Math.sin(lat3Rads)+Math.cos(lat1Rads)*Math.cos(lat3Rads)*Math.cos(dLon)) * EARTH_RADIUS_KM;
     return (Math.abs(Math.asin(Math.sin(distanceAC/EARTH_RADIUS_KM)*Math.sin(Math.toRadians(bearing1)-Math.toRadians(bearing2))) * EARTH_RADIUS_KM));

}

0 个答案:

没有答案