温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用java实现根据一个经纬度查询附近的楼盘信息

发布时间:2021-11-17 11:22:23 来源:亿速云 阅读:387 作者:iii 栏目:大数据

这篇文章主要讲解了“怎么用java实现根据一个经纬度查询附近的楼盘信息”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用java实现根据一个经纬度查询附近的楼盘信息”吧!

实现原理

先算出该点周围的矩形的四个点,然后使用经纬度去直接匹配数据库中的记录。

java代码

/**
     * 根据传入的经纬度和半径范围确定附近的经纬度范围
     *
     * @param longitude 经度
     * @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;
        }

        //先计算查询点的经纬度范围
        double r = 6371;//地球半径千米
        double dlng = 2 * Math.asin(Math.sin(distance / (2 * r)) / Math.cos(latitude * Math.PI / 180));
        dlng = dlng * 180 / Math.PI;//角度转为弧度
        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;
    }

经纬度格式校验

/**
     * 经纬度校验
     * 经度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点经度
     * @param lat1 A点纬度
     * @param lng2 B点经度
     * @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经弧度
        double radiansAY = Math.toRadians(lat1); // A纬弧度
        double radiansBX = Math.toRadians(lng2); // B经弧度
        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; // 最终结果
    }

拿到4个确定范围的经纬度就可以去数据库查询了,由于数据库经纬度存的是一个字段需要切割下字段(使用的是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}

得到的结果进行比较删除

因为得到的结果是个正方形方位内的数据,想要在地图上显示,会发现超过了地图上圆圈外也有数据,这时候就需要再做一下处理。

//判断两点之间的距离是否大于半径,大于的删除
for (int i = 0; i < 之前查询结果的len; i++) {
	double distance = LocationUtil.getDistance(longitude, latitude, longitude1, latitude1);
	if (distance > kilometer) {
		list.remove(i);
		i--;
		len--;
	}
}

感谢各位的阅读,以上就是“怎么用java实现根据一个经纬度查询附近的楼盘信息”的内容了,经过本文的学习后,相信大家对怎么用java实现根据一个经纬度查询附近的楼盘信息这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI