# 取得两点间的GPS距离

www.myexceptions.net  网友分享于：2013-07-04  浏览：40次

# 现在LBS越来越流行，计算地理位置的距离的需求十分普及，以下我就分享一下自己平时用来获得两点间的GPS距离的实用方法：

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];
}```

Java代码

```public static double geo_distance(double lat1, double lng1, double lat2,
double lng2) {
// earth's mean radius in KM
double r = 6378.137;
double d1 = Math.abs(lat1 - lat2);
double d2 = Math.abs(lng1 - lng2);
double p = Math.pow(Math.sin(d1 / 2), 2) + Math.cos(lat1)
* Math.cos(lat2) * Math.pow(Math.sin(d2 / 2), 2);
double dis = r * 2 * Math.asin(Math.sqrt(p));
return dis;
}
```

```//将角度转换为弧度
public static double deg2rad(double degree) {
return degree / 180 * Math.PI;
}
//将弧度转换为角度
return radian * 180 / Math.PI;
}```

```double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
dist = Math.acos(dist);
double miles = dist * 60 * 1.1515;
return miles;}
```

http://search1.mapabc.com/sisserver?highLight=false&config=CDX&ver=2.0&x1= ??? &y1= ??? &x2= ??? &y2= ??? &enc=utf-8&resType=json & a_k= "your key" &ctx=1933347&a_nocache=715403361154