根據地址座標計算距離並排序(java)

業務需求

小程序端有很多門店,需要根據你所在位置就近展示門店信息,並顯示距離;
實現方式可以在小程序端,引用地圖插件根據座標展示距離,但就近展示似乎不好處理(前端人員回饋);
這裏僅描述在後臺如何處理門店就近展示;

後臺代碼

@Override
    public List<TDept> selectByExampleByPort(TDept dept) {
        List<TDept> distanceAndResidueSeat = new ArrayList<>();
        // 數據庫查詢門店數據
        List<TDept> tDepts = tDeptMapper.selectByExampleByPort(dept);
		// 入參座標拼接
        String coordinate = dept.getLatitude()+","+dept.getLongitude();
        if(StringUtils.isNotBlank(dept.getLatitude()) && StringUtils.isNotBlank(dept.getLongitude())){
            //根據地址座標計算距離
            List<TDept> tDepts1 = computeDistance(coordinate, tDepts);
            //根據距離排序
            distanceAndResidueSeat = getDistanceAndResidueSeat(tDepts1);
        }
       	// 返回排序之後的數據集合
        return distanceAndResidueSeat;
    }
    
	/**
     * 根據地址座標計算距離
     * @return
     */
    public List<TDept> computeDistance(String coordinate, List<TDept> tDeptList){
        List<TDept> tDepts = new ArrayList<>();
        if(tDeptList.size() > 0){
            for (TDept tDept : tDeptList) {
                String longitude = tDept.getLongitude();
                String latitude = tDept.getLatitude();
                if(StringUtils.isNotBlank(longitude) && StringUtils.isNotBlank(latitude) && StringUtils.isNotBlank(coordinate)){
                    String distanceData = latitude+","+longitude;
                    double distance = MapHelper.GetPointDistance(coordinate, distanceData);
                    Double format = Double.parseDouble(String.format("%.1f", distance));
                    tDept.setDistance(format);
                    tDepts.add(tDept);
                }
            }
        }
        return tDepts;
    }

	/**
     * 根據距離排序
     * @return
     */
	public static List<TDept> getDistanceAndResidueSeat(List<TDept> list) {
        TDept tDept = null;
        boolean exchange = false;
        for (int i = 0; i < list.size(); i++) {
            for (int j = list.size() - 2; j >= i; j--) {
                if (list.get(j + 1).getDistance() < list.get(j).getDistance()) {
                    tDept = list.get(j + 1);
                    list.set(j + 1, list.get(j));
                    list.set(j, tDept);
                    exchange = true;
                }
            }
            if (!exchange)
                break;
        }
        return list;
    }

通過地圖上的兩個座標計算距離(Java版本)

/**
 * 通過地圖上的兩個座標計算距離(Java版本)
 */
public class MapHelper {
    /**
     * 地球半徑
     */
    private static double EarthRadius = 6378.137;

    /**
     * 經緯度轉化成弧度
     *
     * @param d 經度/緯度
     * @return
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 計算兩個座標點之間的距離
     *
     * @param firstLatitude   第一個座標的緯度
     * @param firstLongitude  第一個座標的經度
     * @param secondLatitude  第二個座標的緯度
     * @param secondLongitude 第二個座標的經度
     * @return 返回兩點之間的距離,單位:公里/千米
     */
    public static double getDistance(double firstLatitude, double firstLongitude,
                                     double secondLatitude, double secondLongitude) {
        double firstRadLat = rad(firstLatitude);
        double firstRadLng = rad(firstLongitude);
        double secondRadLat = rad(secondLatitude);
        double secondRadLng = rad(secondLongitude);

        double a = firstRadLat - secondRadLat;
        double b = firstRadLng - secondRadLng;
        double cal = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(firstRadLat)
                * Math.cos(secondRadLat) * Math.pow(Math.sin(b / 2), 2))) * EarthRadius;
        double result = Math.round(cal * 10000d) / 10000d;
        return result;
    }

    /**
     * 計算兩個座標點之間的距離
     *
     * @param firstPoint  第一個座標點的(緯度,經度) 例如:"31.2553210000,121.4620020000"
     * @param secondPoint 第二個座標點的(緯度,經度) 例如:"31.2005470000,121.3269970000"
     * @return 返回兩點之間的距離,單位:公里/千米
     */
    public static double GetPointDistance(String firstPoint, String secondPoint) {
        String[] firstArray = firstPoint.split(",");
        String[] secondArray = secondPoint.split(",");
        double firstLatitude = Double.valueOf(firstArray[0].trim());
        double firstLongitude = Double.valueOf(firstArray[1].trim());
        double secondLatitude = Double.valueOf(secondArray[0].trim());
        double secondLongitude = Double.valueOf(secondArray[1].trim());
        return getDistance(firstLatitude, firstLongitude, secondLatitude, secondLongitude);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章