Android GPS两点间距离

baacloud免费翻墙vpn注册使用

Android中可采用如下代码获取距离:

Java代码
public double getDistance(double lat1, double lon1, double lat2, double lon2) {
float[] results=new float[1];
Location.distanceBetween(lat1, lon1, lat2, lon2, results);
return results[0];
}
在其他设备若没有类似Android的Location的distanceBetween方法开采用如下代码获取:

Java代码
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);
double miles = dist * 60 * 1.1515;
return miles;
}
//将角度转换为弧度
static double deg2rad(double degree) {
return degree / 180 * Math.PI;
}
//将弧度转换为角度
static double rad2deg(double radian) {
return radian * 180 / Math.PI;
}
这个计算得出的结果是英里,如果要转换成公里,需要乘以1.609344,若是海里需要乘以0.8684

从此学习网 http://www.congci.com/item/android-gps-distance

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏