Search code examples
iosswiftparse-platformgeolocation

Save User Geo Point in Parse


I am trying to save the user's location as a PFgeopoint to a custom parse class named: User. Nothing is saving and I can't find anything in the parse docs to figure out my error. Please help!

  let user = PFObject(className:"User")


        PFGeoPoint.geoPointForCurrentLocationInBackground {
            (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
            if error == nil {
                user.setObject(geoPoint!, forKey: "Location")
            } else {
                user.setObject(geoPoint!, forKey: "Location")
            }
        }

Solution

  • In order to save the user location to parse, you must get it from the device using the CLLocationManagerthen save it into a PFGeoPoint

    var manager = CLLocationManager()
    var loc =  manager.location.coordinate
    var actualLocation = PFGeoPoint(latitude:loc.latitude,longitude:loc.longitude)
     let object = PFObject(className:"User")
     object["Location"] = actualLocation
    object.saveInBackgroundWithBlock { (_success:Bool, _error:NSError?) -> Void in
                if _error == nil
                {
                  // yay its saved
                }