【Android】Parse 开发笔记(3)—— 实现查找附近的功能(LBS)


前言

还在担心Parse不支持复杂的SQL查询,比如实现查找附近的人的功能,今天有认真的看了一遍文章《面向 Android 应用程序的基于 Parse 云的服务》,喜出望外,居然直接提供了API,不愧是专门做移动后台的!

 

声明
欢迎转载,但请保留文章原始出处:)

博客园:http://www.cnblogs.com

农民伯伯: http://over140.cnblogs.com 

 

正文

一、系列

1、【Parse】开发笔记(1)—— 准备

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();
    }

  代码说明:

1、ParseQuery提供了很贴心的方法:whereWithinKilometers(String key, ParseGeoPoint point, double maxDistance) 查找点值在给定点附近,并且在给定最大距离内的对象。 最后一个参数是用来限制范围,单位公里。

2、相关的两个方法: whereWithinRadians和whereWithinMiles,单位不同。

3、ParseGeoPoint这个对象是可以存储的,数据类型为GeoPoint,新增这个字段保存即可。 

 

posted @ 2013-05-31 10:56  农民伯伯  阅读(7887)  评论(0编辑  收藏  举报