美文网首页
计算两个已知经纬度点的距离

计算两个已知经纬度点的距离

作者: YANG_ad29 | 来源:发表于2020-10-24 10:35 被阅读0次

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);
    }

相关文章

网友评论

      本文标题:计算两个已知经纬度点的距离

      本文链接:https://www.haomeiwen.com/subject/jdsimktx.html