4

我一直在到处寻找有关如何使用ch.hsr.geohash搜索附近位置的清晰示例。我的意思是,我只需要实现以下情况:

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

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

4

1 回答 1

8

我以以下方法结束:

地理哈希geohash = Geohash。withCharacterPrecision(纬度,经度,12);字符串 geohashString = geohash.toBase32();

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

数据库:

Name | geohash | id

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

然后我想得到这个点半径100公里内的所有模型(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");

然后它将仅匹配以“gc7”开头的模型 1 和模型 2,因此它们大约在 100 公里半径范围内。

为了精确起见,请遵循此表: https ://en.wikipedia.org/wiki/Geohash

我实际上可以将步骤简化为:

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

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

于 2017-07-17T09:18:34.690 回答