我有 3 种计算距离的方法,所有 3 种方法都给了我不同的答案,
double lat = 6.924049;
double lng = 79.853807;
double lat1 = 6.856461;
double lng1 = 79.912748;
如何计算两点之间的距离以及如何在公里上百分比?还是米?
第一种方式 ans = 9.967441199417708E-6
float[] results = new float[1];
Location.distanceBetween(lat / 1E6, lng / 1E6, lat1 / 1E6, lng1 / 1E6,results);
float s =results[0] * 0.000621371192f;
String a2 = Float.toString(s);
第二种方式ans = 6.1795527E6
double lat3 = lat / 1E6;
double lat2 = lat1 / 1E6;
double lon3 = lng / 1E6;
double lon2 = lng1 / 1E6;
double dLat = Math.toRadians(lat2 - lat3);
double dLon = Math.toRadians(lon2 - lon3);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
// 6378.1 is gravity of earth
double asd = c * 6378.1;
第三种方式=6.176576174444191
{
double a5 = distance(lat, lng, lat1, lng1);
String a6 = Double.toString(a5);
}
private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
那么,如何计算两点之间的距离以及如何在 KM 上百分比呢?还是米?