lbs基于位置服务(Location Based Service)中如何计算二点距离
基于位置的服务(Location Based Service,LBS),它是通过电信移动运营商的无线电通讯网络(如GSM网、CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标),在GIS(Geographic Information System,地理信息系统)平台的支持下,为用户提供相应服务的一种增值业务。
具体代码如下所示:
/**
* 计算两点距离
*/
public class GeoTool {
/**
*
*/
private static final double EARTH_RADIUS = 6378.137;
/***
* 计算两点间的距离
* @param lat1
* @param lon1
* @param lat2
* @param lon2
* @return
*/
public static double getPointDistance(double lat1, double lon1, double lat2, double lon2) {
double result = 0;
double radLat1 = radian(lat1);
double ratlat2 = radian(lat2);
double a = radian(lat1) - radian(lat2);
double b = radian(lon1) - radian(lon2);
result = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLat1) * Math.cos(ratlat2)
* Math.pow(Math.sin(b / 2), 2)));
result = result * EARTH_RADIUS;
result = Math.round(result * 1000);
return result;
}
// 转换为弧度
private static double radian(double d) {
return (d * Math.PI) / 180.00;
}
/**
* 获取geoHash
* @param lat
* @param lon
* @return
*/
public static String geoHash(double lat,double lon){
return Geohash.encode(lat, lon);
}
}