1.计算A,B两点的距离
import java.awt.geom.Point2D;
/**
* 通过AB点经纬度获取距离
* String f 39.908491,116.374328 前面纬度后面经度
*String t 39.108491,116.374328
* @return 距离(单位 : 米)
*/
private static final double EARTH_RADIUS = 6371393;
public static double getDistance(String f, String t) {
String[] splita = StringUtils.split(f, ",");
String[] splitb = StringUtils.split(t, ",");
Point2D pointA = new Point2D.Double(Double.valueOf(splita[1]), Double.valueOf(splita[0]));
Point2D pointB = new Point2D.Double(Double.valueOf(splitb[1]), Double.valueOf(splitb[0]));
double radiansAX = Math.toRadians(pointA.getX()); // A经弧度
double radiansAY = Math.toRadians(pointA.getY()); // A纬弧度
double radiansBX = Math.toRadians(pointB.getX()); // B经弧度
double radiansBY = Math.toRadians(pointB.getY()); // B纬弧度
double cos = Math.cos(radiansAY) * Math.cos(radiansBY) * Math.cos(radiansAX - radiansBX)
+ Math.sin(radiansAY) * Math.sin(radiansBY);
double acos = Math.acos(cos); // 反余弦值
return EARTH_RADIUS * acos; // 最终结果
}
2.根据已知点经纬度,半径距离,随机产生另一个点
/**
* 随机点
* String locaton 39.908491,116.374328 前面纬度后面经度
* int distance 3900 米
*
*/
private static final double EARTH_RADIUS = 6371393;
public static String randomLocation(String locaton, int distance){
String[] splita = StringUtils.split(locaton, ",");
Double startLong = Double.valueOf(splita[1]);
Double startLat = Double.valueOf(splita[0]);
double angle = new Random().nextInt(360); //随机一个偏离角度
DecimalFormat df = new DecimalFormat("0.000000");
//将距离转换成经度的计算公式
double δ = distance/EARTH_RADIUS;
// 转换为radian,否则结果会不正确
angle = Math.toRadians(angle);
startLong = Math.toRadians(startLong);
startLat = Math.toRadians(startLat);
double lat =
Math.asin(Math.sin(startLat)*Math.cos(δ)+Math.cos(startLat)*Math.sin(δ)*Math.cos(angle));
double lon = startLong + Math.atan2(Math.sin(angle)*Math.sin(δ)*Math.cos(startLat),Math.cos(δ)-
Math.sin(startLat)*Math.sin(lat));
// 转为正常的10进制经纬度
lon = Math.toDegrees(lon);
lat = Math.toDegrees(lat);
return df.format(lat)+","+df.format(lon);
}
网友评论