前言
還在擔心Parse不支持復雜的SQL查詢,比如實現查找附近的人的功能,今天有認真的看了一遍文章《面向 Android 應用程序的基於 Parse 雲的服務》,喜出望外,居然直接提供了API,不愧是專門做移動后台的!
聲明
歡迎轉載,但請保留文章原始出處:)
博客園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com
正文
一、系列
2、【Parse】開發筆記(2)—— 從Mysql導入數據到Parse Data
二、簡介
要實現查找附近的人的功能,一般步驟:通過設備定位獲得地理位置信息,上傳到服務端保存數據,通過比較排序獲得數據。
三、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();
}
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,新增這個字段保存即可。