Ani*_*udh 12 android parse-platform
我正在开发一个与Geolocations相关的Android应用程序.我正在使用Parse作为后端.我需要根据用户的当前位置显示结果.
我正在使用此查询:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Restaurants");
ParseGeoPoint sLatlong = new ParseGeoPoint(currentLat, currentLong);
query.whereNear("rest_location", sLatlong);
Run Code Online (Sandbox Code Playgroud)
但结果并没有按距离排序.
有人可以帮我吗.
根据Parse 的Android 文档,默认排序顺序应该已经是按距离(从最近到最远)排序,除非您自己指定另一个排序顺序。
另外,您可能想研究一下这些ParseQuery.whereWithin*方法,因为您可以指定您只需要一定数量的英里、公里等范围内的位置。
如果所有其他方法都失败并且排序顺序不想对返回的位置起作用,您始终可以通过循环ParseObject查询返回的 s 并使用 来自行对它们进行排序ParseObject.getParseGeoPoint("rest_location")。
这将返回一个ParseGeoPoint对象,然后您可以执行以下操作
ParseGeoPoint.distanceInKilometersTo(sLatlong);
Run Code Online (Sandbox Code Playgroud)
获取到用户位置的距离并对其进行排序。
这里有一个示例供参考Comparator:
public class UserLocationComparator implements Comparator<ParseObject> {
private final ParseGeoPoint userLoc;
public UserLocationComparator(ParseGeoPoint userLoc) {
this.userLoc = userLoc;
}
@Override
public int compare(ParseObject po1, ParseObject po2) {
ParseGeoPoint loc1 = po1.getParseGeoPoint("rest_location");
ParseGeoPoint loc2 = po2.getParseGeoPoint("rest_location");
return loc1 == null ?
(loc2 == null ? 0 : 1) :
(loc2 == null ? -1 : Double.compare(userLoc.distanceInKilometersTo(loc1), userLoc.distanceInKilometersTo(loc2)));
}
}
Run Code Online (Sandbox Code Playgroud)
然后假设你有一个List<ParseObject> results;简单的做法:
Collections.sort(results, new UserLocationComparator(sLatlong));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
448 次 |
| 最近记录: |