解析用户周围的位置

时间:2014-09-18 22:57:31

标签: xcode parse-platform

您好我开发了一个应用程序,我想问一个问题PLZ

在我的数据云Parse中,我有类“餐馆”我有三列:“名字”类型字符串; “imageFile”类型文件; “description”类型数组和“Location”类型GeoPoint。

我想知道使用哪种方法来获取用户的当前GeoPoint? 然后我想知道如何比较用户的GeoPoint和餐馆的GeoPoint最终在TableView餐馆中发布(显示)最接近用户的公里数。

1 个答案:

答案 0 :(得分:2)

要获取用户的位置,您可以使用以下功能:PFGeoPoint.geoPointForCurrentLocationInBackground然后您将创建一个查询,使用当前位置查找位于用户所在位置的点。

Swift中的代码示例:

//Get the user's current location
PFGeoPoint.geoPointForCurrentLocationInBackground {
    (point:PFGeoPoint!, error:NSError!) -> Void in
    if error == nil {
        //Point contains the user's current point

        //Get a max of 100 of the restaurants that are within 5km,
        //ordered from nearest to furthest
        var query = PFQuery(className: "restaurants")
        query.limit = 100
        query.whereKey("location", nearGeoPoint: point, withinKilometers: 5.0)
        query.findObjectsInBackgroundWithBlock{
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if (error == nil) {
                //objects contains the restaurants
            }
        }
    }
}