使用 Parse 框架发布持久的 MKPolygon 点
Issue persisting MKPolygon points using Parse framework
我有以下使用 PFGeoPoint
对象将点保存到 Parse.com
的方法:
我使用以下方式捕获它们:
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 分):
这是我用来将点添加到地图的方法:
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
,它缺少很多点并且看起来不完整。
我不太确定这是我保存数据的方式还是我检索数据的方式。
好的,所以我完全重构了将坐标数据保存到 Parse
的方式。
我将列更改为 Array
而不是 GeoPoint
并且它
- 保存速度提高约 300%。
- 正确检索所有坐标。
我有以下使用 PFGeoPoint
对象将点保存到 Parse.com
的方法:
我使用以下方式捕获它们:
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 分):
这是我用来将点添加到地图的方法:
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
,它缺少很多点并且看起来不完整。
我不太确定这是我保存数据的方式还是我检索数据的方式。
好的,所以我完全重构了将坐标数据保存到 Parse
的方式。
我将列更改为 Array
而不是 GeoPoint
并且它
- 保存速度提高约 300%。
- 正确检索所有坐标。