Search code examples
swiftparse-platformmkmapviewpfquerymkpolygon

Issue persisting MKPolygon points using Parse framework


I have the following method that saves points to Parse.com using the PFGeoPoint object:

I capture them using:

func convertPoint(touch: UITouch) {
    let location = touch.locationInView(self.mapView) as CGPoint
    let coordinate: CLLocationCoordinate2D = self.mapView.convertPoint(location, toCoordinateFromView: self.mapView)
    self.coordinates.addObject(NSValue(MKCoordinate: coordinate))
}

Then I use the following to add them to Parse:

func addPolygonToMap() {
    let HUD: MBProgressHUD = showActivityIndicator(true, self.view)

    var numberOfPoints: NSInteger = self.coordinates.count

    if (numberOfPoints > 4) {
        var points: [CLLocationCoordinate2D] = []

        // Save to Parse object.
        var geofenceUserObject = PFObject(className: "GeofenceUser")
        let geofenceId = self.generateUUID()
        geofenceUserObject["UserId"] = "IjlpQHwyfG"
        geofenceUserObject["GeofenceId"] = geofenceId

        geofenceUserObject.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError!) in
            if (error != nil) {
                println("Error saving: \(error)")
            } else if (succeeded) {
                for i in 0..<numberOfPoints {
                    let coordinateValue = self.coordinates[i].MKCoordinateValue

                    points.insert(coordinateValue, atIndex: i)

                    var geoPoint = PFGeoPoint(latitude: coordinateValue.latitude, longitude: coordinateValue.longitude)
                    var geofenceObject = PFObject(className: "GeofenceCoordinates")

                    geofenceObject["Point"] = geoPoint
                    geofenceObject["GeofenceId"] = geofenceId

                    geofenceObject.saveInBackgroundWithBlock({ (operation, error) in
                        println("Saved Geofence objects: \(operation)")

                        println("Points: \(numberOfPoints)")
                        println("Index: \(i+1)")

                        if (i+1 == numberOfPoints) {
                            self.polygon = MKPolygon(coordinates: &points, count: numberOfPoints)
                            self.mapView.addOverlay(self.polygon)

                            self.isDrawingPolygon = false
                            self.createDrawButton("DRAW", color: UIColor(red: 11/255, green: 188/255, blue: 185/255, alpha: 1))
                            self.canvasView.image = nil
                            self.canvasView.removeFromSuperview()

                            HUD.hide(true)
                        }
                    })
                }
            }
        })
    }
}

This is an example MKpolygon that gets created (approx. 319 points):

enter image description here

This is the method I'm using to add the points to the map:

var query = PFQuery(className: "GeofenceCoordinates")
query.orderByAscending("createdAt")
query.whereKey("GeofenceId", equalTo: geofenceId)

query.findObjectsInBackgroundWithBlock({ (objects, error) in
    if let objects = objects as? [PFObject] {
        var coordinates: Array<CLLocationCoordinate2D> = []

        for point in objects {
            if let coordinate = point.objectForKey("Point") as? PFGeoPoint {
                let c = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
                coordinates.append(c)
            }
        }

        let polygon = MKPolygon(coordinates: &coordinates, count: coordinates.count)
        self.mapView.addOverlay(polygon)
    }
})

The problem is that when I retrieve these points from Parse I get the following MKPolygon instead, which is missing quite a few points and looks incomplete.

I'm not quite sure whether it's my way of persisting the data or the way I'm retrieving the data.

enter image description here


Solution

  • Okay, so I completely refactored the way I was saving coordinate data to Parse.

    I changed the column to be an Array instead of a GeoPoint and it

    1. Saves about 300% faster.
    2. Retrieves all the coordinates properly.