java 给出经度纬度坐标计算出2公里范围内的其他坐标

转载https://blog.csdn.net/pavel101/article/details/83585431

 

1,准备一个计算距离的方法

 /**
     *
     * @Description   计算给定经纬度附近相应公里数的经纬度范围
     * @param         longitude 经度
     * @param         latitude 纬度
     * @param         distince 距离(千米)
     **/
    public static HashMap<String, Double> getPeopleNearby(BigDecimal longitude, BigDecimal latitude, Integer distince) {
        double r = 6371.393;    // 地球半径千米
        double lng = longitude.doubleValue();
        double lat = latitude.doubleValue();
        double dlng = 2 * Math.asin(Math.sin(distince / (2 * r)) / Math.cos(lat * Math.PI / 180));
        dlng = dlng * 180 / Math.PI;// 角度转为弧度
        double dlat = distince / r;
        dlat = dlat * 180 / Math.PI;
        double minlat = lat - dlat;
        double maxlat = lat + dlat;
        double minlng = lng - dlng;
        double maxlng = lng + dlng;

        HashMap<String, Double> map = new HashMap<>();
        map.put("minlng", minlng);
        map.put("maxlng", maxlng);
        map.put("minlat", minlat);
        map.put("maxlat", maxlat);
        return map;
    }

 

2,求出几公里内坐标的最大值与最小值

 public static Double getDistince(BigDecimal longitude1, BigDecimal latitude1, BigDecimal longitude2, BigDecimal latitude2) {
        double r = 6371.393;         // 地球半径千米
        double lat1 = latitude1.doubleValue();
        double lng1 = longitude1.doubleValue();
        double lat2 = latitude2.doubleValue();
        double lng2 = longitude2.doubleValue();
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * r;
        s = Math.round(s * 1000);
        return s;
    }
    private static Double rad(double d) {
        return d * Math.PI / 180.0;
    }

简化一下代码 

public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        // 纬度
        double lat1 = (Math.PI / 180) * latitude1;
        double lat2 = (Math.PI / 180) * latitude2;
        // 经度
        double lon1 = (Math.PI / 180) * longitude1;
        double lon2 = (Math.PI / 180) * longitude2;
        // 地球半径
        double R =6371.393; 
// 两点间距离 km,如果想要米的话,结果*1000 

    double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;

    return d
; }

3,得出距离内的坐标后进行距离近的排序

 public List<SiteSurvey> getNearbyStoreByDistinceAsc(BigDecimal longitude, BigDecimal latitude, List<SiteSurvey> nearbyStoreList) {
        List<SiteSurvey> list = new ArrayList<>();
        nearbyStoreList.forEach(siteSurvey -> {
            SiteSurvey siteSurveyNew = new SiteSurvey();
            BeanUtils.copyProperties(siteSurvey, siteSurveyNew);
            Double distince = PeopleNearby.getDistince(longitude, latitude,
                   new BigDecimal(siteSurvey.getLng()), new BigDecimal(siteSurvey.getLat()));
            siteSurveyNew.setDistince(distince.longValue());
            list.add(siteSurveyNew);
        });
        Collections.sort(list, Comparator.comparing(SiteSurvey::getDistince));
        return list;
    }
原文地址:https://www.cnblogs.com/javaLin/p/14209752.html