一、系列
二、简介
要实现查找附近的人的功能,一般步骤:通过设备定位获得地理位置信息,上传到服务端保存数据,通过比较排序获得数据。
三、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,新增这个字段保存即可。