java 根据经纬度 坐标 计算两点之间的距离

根据经纬度,坐标,计算两点之间的距离:

参数格式如下:每一个param的两个值之间用分号分割

@param1  positionOne =aa.bbbb;cc.dddd

@param2  positionOne =ee.bbbb;gg.dddd

public static double getDistance(String positionOne, String positionTwo)

{
double long1;
double lat1;
double long2;
double lat2;
double distance = 0d;
if (positionOne != null && positionOne.length() > 0
&& positionTwo != null && positionTwo.length() > 0)
{
String positionArrayOne[] = positionOne.split(";");
String positionArrayTwo[] = positionTwo.split(";");
try
{
long1 = Double.parseDouble(positionArrayOne[0]);
lat1 = Double.parseDouble(positionArrayOne[1]);
long2 = Double.parseDouble(positionArrayTwo[0]);
lat2 = Double.parseDouble(positionArrayTwo[1]);
if (long1 >= 0 && lat1 >= 0 && long2 >= 0 && lat2 >= 0)
{
double a, b, R;
R = 6378137; // 地球半径
lat1 = lat1 * Math.PI / 180.0;
lat2 = lat2 * Math.PI / 180.0;
a = lat1 - lat2;
b = (long1 - long2) * Math.PI / 180.0;
double sa2, sb2;
sa2 = Math.sin(a / 2.0);
sb2 = Math.sin(b / 2.0);
distance = 2
* R
* Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)
* Math.cos(lat2) * sb2 * sb2));
}
}
catch (Exception e)
{
return distance;
}
}
return distance;
}
上一篇:质量基础设施一站式服务平台建设,NQI系统开发


下一篇:linux之bash介绍