一、系列
二、簡介
要實作查找附近的人的功能,一般步驟:通過裝置定位獲得地理位置資訊,上傳到服務端儲存資料,通過比較排序獲得資料。
三、Mysql版本
典型的SQL語句如:
ORDER BY ABS( locationLatitude - ? + locationLongitude - ?)
(PS~~~,如果資料量大、還關聯多個表,這語句要歇菜鳥~~~)
四、Parse版本
public static List<ParseObject> queryAroundUsers(final Context ctx, POUser user, int minute, int startIndex, int pageSize) throws ParseException {
ParseQuery query = new ParseQuery("nmbb_user");
ParseGeoPoint point = new ParseGeoPoint();
point.setLatitude(user.locationLatitude);
point.setLongitude(user.locationLongitude);
query.whereWithinKilometers("location", point, 5);//最大5公裡
query.setSkip(startIndex);
query.setLimit(pageSize);
return query.find();
}
代碼說明:
1、ParseQuery提供了很貼心的方法:whereWithinKilometers(String key, ParseGeoPoint point, double maxDistance) 查找點值在給定點附近,并且在給定最大距離内的對象。 最後一個參數是用來限制範圍,機關公裡。
2、相關的兩個方法: whereWithinRadians和whereWithinMiles,機關不同。
3、ParseGeoPoint這個對象是可以存儲的,資料類型為GeoPoint,新增這個字段儲存即可。