溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用java實現(xiàn)根據(jù)一個經(jīng)緯度查詢附近的樓盤信息

發(fā)布時間:2021-11-17 11:22:23 來源:億速云 閱讀:420 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“怎么用java實現(xiàn)根據(jù)一個經(jīng)緯度查詢附近的樓盤信息”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用java實現(xiàn)根據(jù)一個經(jīng)緯度查詢附近的樓盤信息”吧!

實現(xiàn)原理

先算出該點周圍的矩形的四個點,然后使用經(jīng)緯度去直接匹配數(shù)據(jù)庫中的記錄。

java代碼

/**
     * 根據(jù)傳入的經(jīng)緯度和半徑范圍確定附近的經(jīng)緯度范圍
     *
     * @param longitude 經(jīng)度
     * @param latitude 緯度
     * @param distance 距離 多少千米
     * @return
     */
    public static Location getNearbyLocation(double longitude, double latitude, double distance) {

        boolean b = LocationUtil.checkItude(longitude + "", latitude + "");

        if (!b) {
            return null;
        }

        //先計算查詢點的經(jīng)緯度范圍
        double r = 6371;//地球半徑千米
        double dlng = 2 * Math.asin(Math.sin(distance / (2 * r)) / Math.cos(latitude * Math.PI / 180));
        dlng = dlng * 180 / Math.PI;//角度轉(zhuǎn)為弧度
        double dlat = distance / r;
        dlat = dlat * 180 / Math.PI;
        double minlat = latitude - dlat;
        double maxlat = latitude + dlat;
        double minlng = longitude - dlng;
        double maxlng = longitude + dlng;

        Location location = new Location();
        location.setLatitude(latitude);
        location.setLongitude(longitude);
        location.setMaxLatitude(maxlat + "");
        location.setMinLatitude(minlat + "");
        location.setMaxLongitude(maxlng + "");
        location.setMinLongitude(minlng + "");
        return location;
    }

經(jīng)緯度格式校驗

/**
     * 經(jīng)緯度校驗
     * 經(jīng)度longitude: (?:[0-9]|[1-9][0-9]|1[0-7][0-9]|180)\\.([0-9]{6})
     * 緯度latitude:  (?:[0-9]|[1-8][0-9]|90)\\.([0-9]{6})
     *
     * @return
     */
    public static boolean checkItude(String longitude, String latitude) {
        String reglo = "((?:[0-9]|[1-9][0-9]|1[0-7][0-9])\\.([0-9]{0,6}))|((?:180)\\.([0]{0,6}))";
        String regla = "((?:[0-9]|[1-8][0-9])\\.([0-9]{0,6}))|((?:90)\\.([0]{0,6}))";
        longitude = longitude.trim();
        latitude = latitude.trim();
        return longitude.matches(reglo) == true ? latitude.matches(regla) : false;
    }

求兩點之間的距離

    /**
     * 求兩點之間的距離
     * @param lng1 A點經(jīng)度
     * @param lat1 A點緯度
     * @param lng2 B點經(jīng)度
     * @param lat2 B點緯度
     * @return 兩點距離
     */
    public static double getDistance(double lng1, double lat1, double lng2, double lat2) {
        double EARTH_RADIUS = 6371;
        double radiansAX = Math.toRadians(lng1); // A經(jīng)弧度
        double radiansAY = Math.toRadians(lat1); // A緯弧度
        double radiansBX = Math.toRadians(lng2); // B經(jīng)弧度
        double radiansBY = Math.toRadians(lat2); // B緯弧度

        // 公式中“cosβ1cosβ2cos(α1-α2)+sinβ1sinβ2”的部分,得到∠AOB的cos值
        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; // 最終結(jié)果
    }

拿到4個確定范圍的經(jīng)緯度就可以去數(shù)據(jù)庫查詢了,由于數(shù)據(jù)庫經(jīng)緯度存的是一個字段需要切割下字段(使用的是Mysql),在 原本的條件下拼接上范圍條件就完成!

條件sql

substring( location, 1, LOCATE ( ',', location ) - 1 ) >= #{minLongitude}
AND substring( location, 1, LOCATE ( ',', location ) - 1 ) <= #{maxLongitude}
AND substring( location, LOCATE ( ',', location ) + 1, LENGTH ( a.location ) - 1 ) >= #{minLatitude}
AND substring( location, LOCATE ( ',', location ) + 1, LENGTH ( a.location ) - 1 ) <= #{maxLatitude}

得到的結(jié)果進行比較刪除

因為得到的結(jié)果是個正方形方位內(nèi)的數(shù)據(jù),想要在地圖上顯示,會發(fā)現(xiàn)超過了地圖上圓圈外也有數(shù)據(jù),這時候就需要再做一下處理。

//判斷兩點之間的距離是否大于半徑,大于的刪除
for (int i = 0; i < 之前查詢結(jié)果的len; i++) {
	double distance = LocationUtil.getDistance(longitude, latitude, longitude1, latitude1);
	if (distance > kilometer) {
		list.remove(i);
		i--;
		len--;
	}
}

感謝各位的閱讀,以上就是“怎么用java實現(xiàn)根據(jù)一個經(jīng)緯度查詢附近的樓盤信息”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對怎么用java實現(xiàn)根據(jù)一個經(jīng)緯度查詢附近的樓盤信息這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI