1、使用geohash.encode();方法计算出当前经纬度的位置编码;
例如:(113.76630306243896,34.772396824521024的编码是t4813ym59twz)
2、字符串相似的表示距离相近,使用sql的like查询't481%'即可获得附近的位置,模糊查询位数约大表示距离约近;

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

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

被折叠的 条评论
为什么被折叠?



