模拟位置信息
程序员文章站
2022-04-03 12:31:16
...
计算两点间距离
方法一
方法二
模拟位置信息的代码在附件里
方法一
double dist = 0.0;
double deltaLat = Math.toRadians(latVal2 - latVal1);
double deltaLon = Math.toRadians(lonVal2 - lonVal1);
latVal1 = Math.toRadians(latVal1);
latVal2 = Math.toRadians(latVal2);
lonVal1 = Math.toRadians(lonVal1);
lonVal2 = Math.toRadians(lonVal2);
double earthRadius = 6371;
double a = Math.sin(deltaLat/2) * Math.sin(deltaLat/2) +
Math.cos(latVal1) * Math.cos(latVal2) * Math.sin(deltaLon/2) * Math.sin
(deltaLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
dist = earthRadius * c;
方法二
private double CalculateHaversineMI(double lat1, double long1, double lat2,double long2) {
double dlong = (long2 - long1) * (Math.PI / 180.0f);
double dlat = (lat2 - lat1) * (Math.PI / 180.0f);
double a = Math.pow(Math.sin(dlat / 2.0), 2)
+ Math.cos(lat1 * (Math.PI / 180.0f))
* Math.cos(lat2 * (Math.PI / 180.0f))
* Math.pow(Math.sin(dlong / 2.0), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = 3956 * c;
return d;
}
模拟位置信息的代码在附件里