0

Hi i developp an application and I would want to ask a question plz

In my data cloud Parse , i have class ” restaurants ” i have three columns : ” name ” type string ; “imageFile” type file ; "description ” type array and "Location" type GeoPoint.

I would want to know which method to use to get back current GeoPoint of the user? Then I would want to know how to compare GeoPoint of users and those of the restaurants to post(show) finally in TableView restaurants the closest to the user in kilometers.

Anton K
  • 4,658
  • 2
  • 47
  • 60

1 Answers1

2

To get the user's position, you can use the following function: PFGeoPoint.geoPointForCurrentLocationInBackground you would then create a query that uses the current location to find points that are near the user's location.

Code Example in 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
            }
        }
    }
}
Dehli
  • 5,950
  • 5
  • 29
  • 44
  • Please i don't very understand , where i add this function ? – Mickael Zana Sep 19 '14 at 11:15
  • This will go in you iOS code (wherever you want to get the objects). Note it runs asynchronously so you'll need to reload the view once objects are retrieved. – Dehli Sep 19 '14 at 11:31