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.csdn.net/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; |