使用Parse框架发出持久化MKPolygon点

时间:2015-03-07 02:20:02

标签: swift parse-platform mkmapview pfquery mkpolygon

我有以下方法使用Parse.com对象将积分保存到PFGeoPoint

我使用以下方式捕获它们:

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))
}

然后我使用以下内容将它们添加到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)
                        }
                    })
                }
            }
        })
    }
}

这是一个创建的示例MKpolygon(约319分):

enter image description here

这是我用来将点添加到地图的方法:

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)
    }
})

问题在于,当我从Parse中检索这些点时,我会得到以下MKPolygon,这会丢失很多点并且看起来不完整。

我不太确定这是我保存数据的方式还是我检索数据的方式。

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,所以我完全重构了将坐标数据保存到Parse的方式。

我将列更改为Array而不是GeoPoint并且

  1. 节省约300%的速度。
  2. 正确检索所有坐标。