tushar tushar - 4 years ago 211
Swift Question

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

I want to add the langitude 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.Thanks in andvance.

Answer Source

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.

//Code snippet might have syntax issues as I don't work in swift but the problem in your code remains the one that I described. (I think this code will work alright for you)
let lati = Double(latitue_filed.text)
let lon = Double(longitudead.text.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
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download