stephen D
stephen D

Reputation: 265

How can I save locations(pins) to a parse server?

I have an app that shows locations in a tableview that can be added via a button on a map. I have trouble saving these to parse (yes I know it is being retired but I have set up a new server that works exactly the same so consider it is parse) also note I have looked at this subject : https://stackoverflow.com/questions/30435362/how-can-i-send-coordinates-to-parse-and-save-it-as-parse-pfgeopoint and even if it looks pretty simple it still has me confused so here is my code in my tableview

var places = [Dictionary<String,String>()]

var activePlace = -1

and here is the code that ads a pin (in the map view controller )

 @IBAction func addSpot(sender: UIBarButtonItem) {


        var newCoordinate2 = self.map.userLocation.location!.coordinate;

        var location = CLLocation(latitude: newCoordinate2.latitude, longitude: newCoordinate2.longitude)

        //title = "new address"

        //try change order start

        let annotation = MKPointAnnotation()

        self.map.addAnnotation(annotation)

        annotation.coordinate = newCoordinate2

        annotation.title = title


        annotation.coordinate = self.map.userLocation.location!.coordinate;

        CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) -> Void in


            var title = ""

            if (error == nil) {

                if let p = placemarks?[0] {

                    var subThouroughfare:String = ""
                    var thouroughfare:String = ""

                    if p.subThoroughfare != nil {

                        subThouroughfare = p.subThoroughfare!

                    }

                    if p.thoroughfare != nil {

                        thouroughfare = p.thoroughfare!

                    }

                    title = "\(subThouroughfare) \(thouroughfare)"


                }

            }

            if title == "" {

                title = "Added \(NSDate())"

            }

            places.append(["name":title,"lat":"\(newCoordinate2.latitude)","lon":"\(newCoordinate2.longitude)"])

            let annotation = MKPointAnnotation()

            annotation.coordinate = newCoordinate2

            annotation.title = title

            self.map.addAnnotation(annotation)

        }

        self.map.addAnnotation(annotation);





        func mapView(mapView: MKMapView!,
                     viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!{
            if(annotation is MKUserLocation){
                return nil;
            }

           // let pinView: Void = mapView.addAnnotation(annotation);
            let pinAnnotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier:"MyIdentifier");
            return pinAnnotationView;


        }



    }

thank you for helping as I am new to using parse / external servers

EDIT: I have tried this:

    PFObject["geoPoint"] = PFGeoPoint(location: location)

    location[location] = activePlace
    location.saveInBackgroundWithBlock { (succes, error) -> Void in
        print("place has been saved")
    }

I get errors on the three lines 1) instance member 'subscript' cannot be used on type 'PFObject' 2) type 'CLLocation' has no subscript members 3) value of type CLLocation has no member 'saveInBackgroundWithBlock'

RE EDIT: I have tried this and get a warning saying 'code after return will never be executed' also it runs on the app but doesn't save

    let geoPoint = PFObject(className: "location")

    geoPoint["location"] = activePlace
    geoPoint.saveInBackgroundWithBlock { (succes, error) -> Void in
        print("place has been saved")
    }

Upvotes: 0

Views: 234

Answers (1)

Wain
Wain

Reputation: 119031

So you need something to contain your PFGeoPoint, and that thing needs to be a PFObject. In the parse back end you can create the table for this object and set it up with a reference to a geoPoint and whatever else you want. Now you can instantiate a PFObject for each new point with the class name of the table, set the reference to the geoPoint (with pfObject["geoPoint"] = geoPoint), save it (with pfObject.saveInBackgroundWithBlock...) and you're done.

Upvotes: 1

Related Questions