我以以下方法结束:
地理哈希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 代替。