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)
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
Ok, 당신은'object (forKey :) '를 사용하여 cloudKit에서 레코드를 읽은 다음 그것을'CLLocation'으로 캐스팅하려고합니다. –
내 대답에 해결책을 추가했습니다. – Erva