/**
* 高德地图工具
*
* @author henshuia
*
*/
public class AMapUtils
{
/**
* 根据用户的起点和终点经纬度计算两点间距离,此距离为相对较短的距离,单位米。
*
* @param start
* 起点的坐标
* @param end
* 终点的坐标
* @return
*/
public static double calculateLineDistance(LngLat start, LngLat end)
{
if ((start == null) || (end == null))
{
throw new IllegalArgumentException("非法坐标值,不能为null");
}
double d1 = 0.01745329251994329D;
double d2 = start.longitude;
double d3 = start.latitude;
double d4 = end.longitude;
double d5 = end.latitude;
d2 *= d1;
d3 *= d1;
d4 *= d1;
d5 *= d1;
double d6 = Math.sin(d2);
double d7 = Math.sin(d3);
double d8 = Math.cos(d2);
double d9 = Math.cos(d3);
double d10 = Math.sin(d4);
double d11 = Math.sin(d5);
double d12 = Math.cos(d4);
double d13 = Math.cos(d5);
double[] arrayOfDouble1 = new double[3];
double[] arrayOfDouble2 = new double[3];
arrayOfDouble1[0] = (d9 * d8);
arrayOfDouble1[1] = (d9 * d6);
arrayOfDouble1[2] = d7;
arrayOfDouble2[0] = (d13 * d12);
arrayOfDouble2[1] = (d13 * d10);
arrayOfDouble2[2] = d11;
double d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0])
+ (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1])
+ (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));
return (Math.asin(d14 / 2.0D) * 12742001.579854401D);
}
}
public final class LngLat implements Cloneable
{
/**
* 纬度 (垂直方向)
*/
public double latitude;
//public final double latitude;
/**
* 经度 (水平方向)
*/
public double longitude;
//public final double longitude;
/**
* 格式化
*/
private static DecimalFormat format = new DecimalFormat("0.000000", new DecimalFormatSymbols(Locale.US));
public LngLat() {
}
/**
* 使用传入的经纬度构造LatLng 对象,一对经纬度值代表地球上一个地点。
*
* @param longitude
* 地点的经度,在-180 与180 之间的double 型数值。
* @param latitude
* 地点的纬度,在-90 与90 之间的double 型数值。
*/
public LngLat(double longitude, double latitude)
{
this(longitude, latitude, true);
}
/**
* 使用传入的经纬度构造LatLng 对象,一对经纬度值代表地球上一个地点
*
* @param longitude
* 地点的经度,在-180 与180 之间的double 型数值。
*
* @param latitude
* 地点的纬度,在-90 与90 之间的double 型数值。
* @param isCheck
* 是否需要检查经纬度的合理性,建议填写true
*/
public LngLat(double longitude, double latitude, boolean isCheck)
{
if (isCheck)
{
if ((-180.0D <= longitude) && (longitude < 180.0D))
this.longitude = parse(longitude);
else
{
throw new IllegalArgumentException("the longitude range [-180, 180].");
// this.longitude = parse(((longitude - 180.0D) % 360.0D + 360.0D) %
// 360.0D - 180.0D);
}
if ((latitude < -90.0D) || (latitude > 90.0D))
{
throw new IllegalArgumentException("the latitude range [-90, 90].");
}
this.latitude = latitude;
// this.latitude = parse(Math.max(-90.0D, Math.min(90.0D, latitude)));
}
else
{
this.latitude = latitude;
this.longitude = longitude;
}
}
/**
* 解析
*
* @param d
* @return
*/
private static double parse(double d)
{
return Double.parseDouble(format.format(d));
}
public LngLat clone()
{
return new LngLat(this.latitude, this.longitude);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(latitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(longitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
LngLat other = (LngLat) obj;
if (Double.doubleToLongBits(latitude) != Double.doubleToLongBits(other.latitude))
return false;
if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude))
return false;
return true;
}
public String toString()
{
return "lat/lng: (" + this.latitude + "," + this.longitude + ")";
}
}
测试类
public class Test {
public static void main(String[] args) {
LngLat start = new LngLat(1.368904, 39.923423);
LngLat end = new LngLat(116.387271, 39.922501);
System.err.println(AMapUtils.calculateLineDistance(start, end));
}
}