代码如下:
import 'dart:math';
void main() {
print("distance between two point is : ${getDistance(
119.9831030000, 30.2340350000, 119.9809580000, 30.2320980000)}");
}
getDistance(double lat1, double lng1, double lat2, double lng2) {
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
double s = 2 *
asin(sqrt(pow(sin(a / 2), 2) +
cos(radLat1) * cos(radLat2) * pow(sin(b / 2), 2)));
return s * 6378138.0;
}
double rad(double d) {
return d * pi / 180.0;
}
选取两点作为例子:
- 赞成乐山红叶北门(Latitude: 119.9831030000, Longitude: 30.2340350000)
- 闲林街道办事处(Latitude: 119.9809580000, Longitude: 30.2320980000)
运行结果:
distance between two point is : 261.9676660067193
从一个测经纬度距离的网站测试的结果如下: