如何为注释创建数组

问题描述 投票:0回答:1

并非我的所有注释都会显示在我的地图中,因为我无法在获取CKrecords(注释)时获取var annotation = MKPoinatAnnotation相同的MKPointAnnotation,也无法获取注释的方向。我对如何为注释创建数组感到困惑,因此我可以从CloudKit数据库加载所有注释,并且能够在选择注释时获取方向。

let annotation = MKPointAnnotation()
let database = CKContainer.default().publicCloudDatabase
var truck: [CKRecord] = []

func fetch() {

let truePredicate = NSPredicate(value: true)
let eventQuery = CKQuery(recordType: "User", predicate: truePredicate)
let queryOperation = CKQueryOperation(query: eventQuery)


queryOperation.recordFetchedBlock = { (record : CKRecord!) in

    self.truck.append(record)

    self.annotation.title = record["username"] as? String
    self.annotation.subtitle = record["hours"] as? String
    if let location = record["location"] as? CLLocation {
        self.annotation.coordinate = location.coordinate
    }

    print("recordFetchedBlock: \(record)")


    self.mapView.addAnnotation(self.annotation)  

}

self.database.add(queryOperation)

}

我怎么得到方向 -

@IBAction func getDirections(_ sender: Any) {
let view = annotation.coordinate

print("Annotation: \(String(describing: view ))")

let currentLocMapItem = MKMapItem.forCurrentLocation()

let selectedPlacemark = MKPlacemark(coordinate: view, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark)

let mapItems = [selectedMapItem, currentLocMapItem]

let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]

MKMapItem.openMaps(with: mapItems, launchOptions:launchOptions)
}
ios arrays swift cloudkit mkannotation
1个回答
0
投票

我假设数组的内容根据搜索查询而改变。如果是这种情况,请创建一个MKPointAnnotation类型的数组:

var points: [MKPointAnnotation] = []

然后填充数组,然后填充它并通过循环运行它,每次迭代都会向地图添加一个点:

for point in points {
    mapView.addAnnotation(point)
}

如果您对不同类型的注释有问题,请创建CLLocationCoordinate2D类型的数组,然后您可以通过访问MKPointAnnotation.coordinate来填充数组。

这有帮助吗?

© www.soinside.com 2019 - 2024. All rights reserved.