Geohash-Java搜索附近的LatLongs

时间:2017-07-13 21:31:39

标签: java geolocation geohashing

我一直在寻找一个明确的示例,说明如何使用ch.hsr.geohash搜索附近的位置。我的意思是,我只需要实现以下情况:

1 - 我的纬度很长,经度很长。 2 - 将此纬度/经度转换为哈希值。 (我相信它是通过使用“GeoHash.withBitPrecision(纬度,经度,64)”来完成的,但我不确定这里的精度是多少。我认为64位将具有相同的精度。 3 - 检索100公里距离内的纬度/经度列表(我不知道如何使用geohash启动它) 4 - 使用纬度/经度结果列表查询物体对象。

但我找不到任何关于lib的文档也没有任何明确的例子。 请有人建议我或给我任何起点来搜索它?有没有人已经使用这个库来实现我正在寻找的相同结果? 非常感谢!

1 个答案:

答案 0 :(得分:3)

我结束了以下方法:

Geohash geohash = Geohash。 withCharacterPrecision(纬度,经度,12); String geohashString = geohash.toBase32();

然后根据我想要的距离使用geohashString。我的意思是,根据字符串的精度匹配字符串的前缀。例如,

数据库:

Name | geohash | id

Model 1 | gc7x9813vx2r | 1
Model 2 | gc7x8840suhr | 2
Model 3 | gc30psvp0zgr | 3

然后我想让所有模型在这个点的100km范围内(53.244664,-6.140530)。

Geohash geohash = Geohash. withCharacterPrecision(53.244664, -6.140530, 12);
String geohashString = geohash.toBase32().substring(0, 3); //3 characters for around 100km of precision

ofy().load().type(Model.class).filter("geohash >=", geoHashString).filter("geohash <", geoHashString + "\uFFFD");

然后它只匹配模型1和模型2,它以“gc7”开头,因此它们在半径100km范围内。

遵循此表的精度: https://en.wikipedia.org/wiki/Geohash

我实际上可以简化以下步骤:

String geohashString = Geohash. withCharacterPrecision(53.244664, -6.140530, 3).toBase32();

虽然我没有进行任何性能测试,但我认为它不会慢得多。无论如何,如果性能测试“失败”,我将实现相同的,但改为使用binaryString。