需求:
- 在做移动端附近的人的功能时,通过高德定位模块得到对应的经纬度信息,但是需要知道两个点之间的距离
1、代码来源:
在Maven仓库中找到7.9.1的jar包,jar中有AMapUtils的工具类
implementation 'com.amap.api:3dmap:7.9.1'
2、二次封装
public class AMapUtils {
public static double calculateLineDistance(LatLng var0, LatLng var1) {
if (var0 != null && var1 != null) {
return calculateLineDistance(var0.latitude, var0.longitude, var1.latitude, var1.longitude);
} else {
try {
throw new Exception("非法坐标值");
} catch (Exception var27) {
var27.printStackTrace();
return 0.0;
}
}
}
public static double calculateLineDistance(double latitude1,
double longitude1,
double latitude2,
double longitude2) {
try {
double var2 = longitude1;
double var4 = latitude1;
double var6 = longitude2;
double var8 = latitude2;
var2 *= 0.01745329251994329D;
var4 *= 0.01745329251994329D;
var6 *= 0.01745329251994329D;
var8 *= 0.01745329251994329D;
double var10 = Math.sin(var2);
double var12 = Math.sin(var4);
double var14 = Math.cos(var2);
double var16 = Math.cos(var4);
double var18 = Math.sin(var6);
double var20 = Math.sin(var8);
double var22 = Math.cos(var6);
double var24 = Math.cos(var8);
double[] var28 = new double[3];
double[] var29 = new double[3];
var28[0] = var16 * var14;
var28[1] = var16 * var10;
var28[2] = var12;
var29[0] = var24 * var22;
var29[1] = var24 * var18;
var29[2] = var20;
return (Math.asin(Math.sqrt((var28[0] - var29[0]) * (var28[0] - var29[0]) + (var28[1] - var29[1]) * (var28[1] - var29[1]) + (var28[2] - var29[2]) * (var28[2] - var29[2])) / 2.0D) * 1.27420015798544E7D);
} catch (Throwable var26) {
var26.printStackTrace();
return 0.0;
}
}
}
public final class LatLng implements Cloneable {
public final double latitude;
public final double longitude;
public LatLng(double latitude, double longitude) {
this(latitude, longitude, true);
}
public LatLng(double latitude, double longitude, boolean var5) {
if (!var5) {
this.latitude = latitude;
this.longitude = longitude;
} else {
if (-180.0D <= longitude && longitude < 180.0D) {
this.longitude = longitude;
} else {
this.longitude = ((longitude - 180.0D) % 360.0D + 360.0D) % 360.0D - 180.0D;
}
if (latitude < -90.0D || latitude > 90.0D) {
try {
throw new Exception("非法坐标值");
} catch (Exception var6) {
var6.printStackTrace();
}
}
this.latitude = Math.max(-90.0D, Math.min(90.0D, latitude));
}
}
public final LatLng clone() {
return new LatLng(this.latitude, this.longitude);
}
public final int hashCode() {
long var2 = Double.doubleToLongBits(this.latitude);
int var1 = 31 + (int) (var2 ^ var2 >>> 32);
var2 = Double.doubleToLongBits(this.longitude);
return 31 * var1 + (int) (var2 ^ var2 >>> 32);
}
public final boolean equals(Object var1) {
if (this == var1) {
return true;
} else if (!(var1 instanceof LatLng)) {
return false;
} else {
LatLng var2 = (LatLng) var1;
return Double.doubleToLongBits(this.latitude) == Double.doubleToLongBits(var2.latitude) && Double.doubleToLongBits(this.longitude) == Double.doubleToLongBits(var2.longitude);
}
}
public final String toString() {
return "lat/lng: (" + this.latitude + "," + this.longitude + ")";
}
public final int describeContents() {
return 0;
}
}
3、使用
public class Main {
public static void main(String[] args) {
double lat1 = 39.923423, lng1 = 116.368904;
double lat2 = 39.922501, lng2 = 116.387271;
double v = AMapUtils.calculateLineDistance(new LatLng(lat1, lng1), new LatLng(lat2, lng2));
System.out.println("通过LatLng对象计算:两个点之间的距离为:" + v + " 米");
double v1 = AMapUtils.calculateLineDistance(lat1, lng1, lat2, lng2);
System.out.println("直接传入经纬度计算:两个点之间的距离为:" + v1 + " 米");
}
}
4、计算结果
- 本文地址:https://blog.csdn.net/qq_40183053/article/details/117916248