swiftparse-platformmkannotationpfobjectgeopoints

How do I save a MKPointAnnotation to Pars?


How do I save on Parse a MKPointAnnotation that I created?

@IBAction func salvaRicordo(sender: AnyObject) {
    let puntoRicordo = MKPointAnnotation()
    puntoRicordo.coordinate = posizioneUtente
    puntoRicordo.title = nomeField.text
    puntoRicordo.subtitle = descrizioneField.text
    self.myMapView.addAnnotation(puntoRicordo)
    print("PointAnnotation creato")
    puntoRicordo.saveInBackgroundWithBlock {
    (success: Bool, error: NSError?) -> Void in
    if (success) {
        // The object has been saved.
    } else {
        // There was a problem, check error.description
    }
}

Solution

  • You can't save a MKPointAnnotation to Parse directly. You may want to save its properties such as coordinate, title and subtitle.

    If you want to save coordinates, you should split them into latitudes and longitudes.

    In Parse you can create a new class, call it "Annotations", add columns named "Title", "Subtitle", "Latitude" and "Longitude", and save your data this way:

    let myObject = PFObject(className: "Annotations")
    myObject["Title"] = nomeField.text
    myObject["Subtitle"] = descrizioneField.text
    myObject["Latitude"] = puntoRicordo.latitude
    myObject["Longitude"] = puntoRicordo.longitude
    myObject.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            print("Data saved to Parse.")
        } else {
            print(error)
        }
    }