2016-11-27 15 views
0

위치 데이터 유형 ("Koordinaatit")으로 CloudKit에 좌표가 있습니다. 이 좌표에서 매핑하려면 MKPointAnnotation 그리려면 노력하고 있지만 배열에서 주석을 얻는 방법을 모르겠습니다.CloudKit의 MKPointAnnotation 좌표

var sijainnitArray:[CKRecord] = [] 


    func drawPin(sijainti: Int) { 

     let annotation = MKPointAnnotation() 
     annotation.coordinate = CLLocationCoordinate2D(sijainnitArray[sijainti].Koordinaatit) //XCode shows error: Value of type 'CKRecord' has no memeber 'Koordinaatit' 
     self.mapView.addAnnotation(annotation) 
} 


func fetchRecordsFromCloud() { 

     let cloudContainer = CKContainer.default() 
     let publicDatabase = cloudContainer.publicCloudDatabase 
     let predicate = NSPredicate(value: true) 
     let query = CKQuery(recordType: "Sijainti", predicate: predicate) 

     publicDatabase.perform(query, inZoneWith: nil, completionHandler: { 
      (results, error) -> Void in 

      if error != nil { 
       print(error) 
       return 
      } 

      if let results = results { 
       print("Completed the download of locations data") 
       self.sijainnitArray = results 
       } 
     }) 

    } 



override func viewDidLoad() { 
    super.viewDidLoad() 

    fetchRecordsFromCloud() 

    let sijainnitLkm = sijainnitArray.count - 1 

    for i in 0...sijainnitLkm { 
     drawPin(sijainti: i) 
     } 
} 



**ANSWER** 

let sijaintilista = self.sijainnitArray[i] 

      let sijaintiLatLong = sijaintilista.object(forKey: "Koordinaatit") as? CLLocation 
      let sijaintiLat = sijaintiLatLong?.coordinate.latitude 
      let sijaintiLon = sijaintiLatLong?.coordinate.longitude 

      let sourceLocation = CLLocationCoordinate2D(latitude: sijaintiLat!, longitude: sijaintiLon!) 
      let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil) 

      if let location = sourcePlacemark.location { 
       annotation.coordinate = location.coordinate 
      } 

      self.mapView.addAnnotation(annotation) 

답변

0

당신은 당신의 CKRecord의 방법 object(forKey:)을 사용하고 싶습니다. 아마 당신이 사용하고자하는 열쇠는 "Koordinaatit"입니다.

이전에 CloudKit을 사용하지 않았습니다. CLLocation을 CKRecord에 직접 저장할 수있는 것 같습니다.

+0

locationManager 위치가 CloudKit CKRecord에 CLLocation으로 저장되어 있는데 문제는 CELLocation을 MKPointAnnotation에 추가하는 방법을 모르겠다는 것입니다. let location = locations.last! publicDatabase = CKContainer.default() publicCloudDatabase 이 할 수 있도록 기록 = CKRecord (RECORDTYPE : "Sijainti"). record.setObject (위치, forKey : "Koordinaatit") publicDatabase.save (기록, completionHandler : { (record, error) -> void in – Erva

+0

Ok, 당신은'object (forKey :) '를 사용하여 cloudKit에서 레코드를 읽은 다음 그것을'CLLocation'으로 캐스팅하려고합니다. –

+0

내 대답에 해결책을 추가했습니다. – Erva