通過經(jīng)緯度判斷兩點的距離 并做離我最近排序

public class DecimalFormatUtil {

    //地球平均半徑
    private static final double EARTH_RADIUS = 6378.137;

    //把經(jīng)緯度轉(zhuǎn)為度(°)
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 通過經(jīng)緯度獲取距離(單位:)
     *  ulongitude  用戶所在 -- 經(jīng)度
     *  ulatitude   用戶所在 -- 緯度
     *  latitude    發(fā)布等   -- 緯度
     *  longitude   發(fā)布等   -- 經(jīng)度
     * @return 距離
     */
    public static double getDistance(double ulongitude, double longitude,double ulatitude, double latitude) {

        double radLat1 = rad(ulongitude);
        double radLat2 = rad(longitude);

        double a = radLat1 - radLat2;
        double b = rad(ulatitude) - rad(latitude);

        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 * EARTH_RADIUS;
        s = s / 1000;
        DecimalFormat df = new DecimalFormat("#.00");
        s = Double.parseDouble(df.format(s));
        return s;
    }


    public static void main(String[] args) {
        double distance1 = getDistance(104.046115, 30.6030110000, 104.074666, 30.611842);
        System.out.println("Distance is: " + distance1 + " km");
    }
}

SQL

SELECT 
    title, 
    longitude, 
    latitude, 
    ROUND( 
        6378.138 * 2 * ASIN( 
            SQRT( 
                POW( 
                    SIN( 
                        ( 
                            30.611842 * PI() / 180 - latitude * PI() / 180 
                        ) / 2 
                    ), 
                    2 
                ) + COS(30.611842 * PI() / 180) * COS(latitude * PI() / 180) * POW( 
                    SIN( 
                        ( 
                            104.074666 * PI() / 180 - longitude * PI() / 180 
                        ) / 2 
                    ), 
                    2 
                ) 
            ) 
        ) * 1000 
    ) AS distance_um 
FROM 
    mac_rent_in 
ORDER BY 
    distance_um ASC 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容