2015-03-07 5 views
0

я следующий метод, который сохраняет точки в Parse.com с помощью PFGeoPoint объекта:выпуска сохраняющегося MKPolygon точка с использованием рамки Синтаксической

Я захватить их с помощью:

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

ответ

0

Хорошо, так что я полностью переработан так, как я спасал данные координат в Parse.

Я изменил столбец быть Array вместо GeoPoint и

  1. экономит около 300% быстрее.
  2. Правильно извлекает все координаты.