Search code examples
iosswiftparse-platformcoordinate-systems

how to save the co-ordinate as PFGeopoint with ios parse.com


I want to add the longitude and latitude from the user using textfield and that value to pass in PFGeoPoint.But Textfield value is in string that not consideras PFGeoPoint. My code is:

@IBOutlet weak var latitue_filed: UITextField!
@IBOutlet weak var longitudead: UITextField!

   let lati :  = (latitue_filed.text as? PFGeoPoint)!
   let lon :  = (longitudead.text as? PFGeoPoint)!

  let myGeoPoint = PFGeoPoint(latitude: lat, longitude: lon)
    posts["location"] = myGeoPoint
    posts.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            // The object has been saved.
        } else {
            // There was a problem, check error.description
        }
    }
    posts.saveInBackground()` 

Here latitue_filed and longitudead is textfield. How can i pass textfield value in PfGeopint.


Solution

  • According to documentation, PFGeoPoint is actually a PFObject which uses doubles latitude and longitude (Which are degrees) and embeds them together. You are currently casting string values from textfield to individual PFGeoPoints. Cast the string values to double and use the method described in the documentation to create a PFGeoPoint.

    let lati = Double(latitue_filed.text)
    let lon = Double(longitudead.text)
    
    let myGeoPoint = PFGeoPoint(latitude: lat, longitude: lon)
    posts["location"] = myGeoPoint
    posts.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            // The object has been saved.
        } else {
            // There was a problem, check error.description
        }
    }