使用geohash实现附近位置检索并且计算与当前位置的距离

本文介绍了如何利用geohash编码技术来实现附近位置的检索。通过geohash.encode()方法计算经纬度编码,使用sql的like查询找到相近位置。当位置在格子边界两侧时,需要搜索周围8个格子,可借助expand.calculateAdjacent()方法。通过foreach结合or查询9个格子编码,并用DisUtil.GetDistance()计算实际距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用geohash.encode();方法计算出当前经纬度的位置编码;

例如:(113.76630306243896,34.772396824521024的编码是t4813ym59twz)

2、字符串相似的表示距离相近,使用sql的like查询't481%'即可获得附近的位置,模糊查询位数约大表示距离约近;

3、如果位置位于格子边界两侧的两点,距离非常近,但是位置编码大不相同,可同时搜索当前格子周围的8个格子。使用expand.calculateAdjacent();方法获取当前位置周围8个格子。

4sql中使用foreach 结合or查询9个格子的位置编码;

5、最后使用DisUtil.GetDistance()获取位置的距离;

参考文章https://blog.youkuaiyun.com/shixiaoguo90/article/details/23909687

public class geohash  {

 

    private static int numbits = 6 * 5;

    final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h',

           'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

 

    final static HashMap<Character, Integer> lookup = new HashMap<Character, Integer>();

    static {

       int i = 0;

       for (char c : digits)

           lookup.put(c, i++);

    }

 

    public double[] decode(String geohash) {

       StringBuilder buffer = new StringBuilder();

       for (char c : geohash.toCharArray()) {

 

           int i = lookup.get(c) + 32;

           buffer.append(Integer.toString(i, 2).substring(1));

       }

 

       BitSet lonset = new BitSet();

       BitSet latset = new BitSet();

 

       // even bits

       int j = 0;

       for (int i = 0; i < numbits * 2; i += 2) {

           boolean isSet = false;

           if (i < buffer.length())

              isSet = buffer.charAt(i) == '1';

           lonset.set(j++, isSet);

       }

 

       // odd bits

       j = 0;

       for (int i = 1;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值