计算
package com.ts.utils;
import com.ts.bean.City;
import com.ts.bean.Location;
public class RoundDistance {
// 地球赤道半径
private static final double ERATH_EQUATORIAL_RADIUS = 6378.1370D;
private static final double CONCVERT_DEGREES_TO_RADIANS = Math.PI / 180;
/**
* 计算传入城市与当前城市的实际距离
*
* @return
*/
public static double measureDistance(Location l1, Location l2) {
double deltaLongitude = deg2rad(l1.getLongitude() - l2.getLongitude());
double deltaLatitude = deg2rad(l1.getLatitude() - l2.getLatitude());
double a = Math.pow(Math.sin(deltaLatitude / 2D), 2D)
+ Math.cos(deg2rad(l2.getLatitude()))
* Math.cos(deg2rad(l1.getLatitude()))
* Math.pow(Math.sin(deltaLongitude / 2D), 2D);
return ERATH_EQUATORIAL_RADIUS * 2D * Math.atan2(Math.sqrt(a), Math.sqrt(1D - a));
}
// 转化为弧度
private static double deg2rad(double deg) {
return deg * CONCVERT_DEGREES_TO_RADIANS;
}
}
抽象类保存经纬度
package com.ts.bean;
public abstract class Location {
// 经度
double longitude;
// 纬度
double latitude;
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
}
评论区