String loc_expr = "distance(location, geopoint(" + userLatitude + ", " + userLongitude + "))";
// Build the SortOptions
SortOptions sortOptions = SortOptions.newBuilder()
.addSortExpression(SortExpression.newBuilder().setExpression(loc_expr).setDirection(SortExpression.SortDirection.ASCENDING).setDefaultValueNumeric(0))
.setLimit(200).build();
// Build the QueryOptions
QueryOptions options = QueryOptions.newBuilder().addExpressionToReturn(FieldExpression.newBuilder().setExpression(loc_expr).setName("distance")).setLimit(limit)
.setCursor(cursor).setSortOptions(sortOptions).build();
String queryString = loc_expr + " < " + searchRadius * 1000;
// Build the Query and run the search
Query query = Query.newBuilder().setOptions(options).build(queryString);
IndexSpec indexSpec = IndexSpec.newBuilder().setName("restaurants").build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
Results<ScoredDocument> result = index.search(query);
if (result.getNumberFound() > 0) {
Collection<ScoredDocument> coll = result.getResults();
for (ScoredDocument sd : coll) {
Key<Restaurant> key = Key.create(String.valueOf(sd.getId()));
Restaurant rest = ofy().load().key(key).now();
Field f = sd.getExpressions().get(0);
log.info("distance in meter : " + f.getNumber());
}
}
我正在使用上述代码获取附近地区的餐馆。以下是我的观察结果:-
情况1:searchRadius=0.5 km---距离最大值=0.9 km
情况2:searchRadius=1 km---距离最大值=1.8 km
情况3:searchRadius=2 km---距离最大值=2.8 km
情况4:searchRadius=3 km---距离最大值=4.8 km
为什么我得到的距离值大于指定的半径?
注意:-我不是自己计算距离的。搜索API正在返回距离。
这是一个已知的问题。
在谷歌修复之前,你必须在你的代码中过滤结果