标签:south 半径 map href anti logs 根据 返回 comment
在项目开发过程中,需要根据两地经纬度坐标计算两地间距离,所用的工具类如下:
public static double getDistatce(double lat1, double lat2, double lon1, double lon2) {
double R = 6371;
double distance = 0.0;
double dLat = (lat2 - lat1) * Math.PI / 180;
double dLon = (lon2 - lon1) * Math.PI / 180;
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(lat1 * Math.PI / 180)
* Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
distance = (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))) * R;
return distance;
}
private static final double EARTH_RADIUS = 6378.137 * 1000;
private static double rad(double d)
{
return d * Math.PI / 180.0;
}
public static double 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 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s * EARTH_RADIUS ;
s = Math.round(s * 10000) / 10000;
return s;
}
private static final double EARTH_RADIUS = 6378137;//赤道半径(单位m)
/**
* 转化为弧度(rad)
* */
private static double rad(double d)
{
return d * Math.PI / 180.0;
}
/**
* 基于余弦定理求两经纬度距离
* @param lon1 第一点的精度
* @param lat1 第一点的纬度
* @param lon2 第二点的精度
* @param lat3 第二点的纬度
* @return 返回的距离,单位km
* */
public static double LantitudeLongitudeDist(double lon1, double lat1,double lon2, double lat2) {
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double radLon1 = rad(lon1);
double radLon2 = rad(lon2);
if (radLat1 < 0)
radLat1 = Math.PI / 2 + Math.abs(radLat1);// south
if (radLat1 > 0)
radLat1 = Math.PI / 2 - Math.abs(radLat1);// north
if (radLon1 < 0)
radLon1 = Math.PI * 2 - Math.abs(radLon1);// west
if (radLat2 < 0)
radLat2 = Math.PI / 2 + Math.abs(radLat2);// south
if (radLat2 > 0)
radLat2 = Math.PI / 2 - Math.abs(radLat2);// north
if (radLon2 < 0)
radLon2 = Math.PI * 2 - Math.abs(radLon2);// west
double x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1);
double y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1);
double z1 = EARTH_RADIUS * Math.cos(radLat1);
double x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2);
double y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2);
double z2 = EARTH_RADIUS * Math.cos(radLat2);
double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)+ (z1 - z2) * (z1 - z2));
//余弦定理求夹角
double theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS));
double dist = theta * EARTH_RADIUS;
return dist;
}
//google map
private static final double EARTH_RADIUS = 6378137;//赤道半径(单位m)
/**
* 转化为弧度(rad)
* */
private static double rad(double d)
{
return d * Math.PI / 180.0;
}
/**
* 基于googleMap中的算法得到两经纬度之间的距离,计算精度与谷歌地图的距离精度差不多,相差范围在0.2米以下
* @param lon1 第一点的精度
* @param lat1 第一点的纬度
* @param lon2 第二点的精度
* @param lat3 第二点的纬度
* @return 返回的距离,单位km
* */
public static double GetDistance(double lon1,double lat1,double lon2, double lat2)
{
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lon1) - rad(lon2);
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2)+Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s;
}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://www.cnblogs.com/captainbed
标签:south 半径 map href anti logs 根据 返回 comment
原文地址:https://www.cnblogs.com/shaoye007/p/10196530.html