gpt4 book ai didi

ios - 如何为注释创建一个数组

转载 作者:行者123 更新时间:2023-11-28 06:07:07 24 4
gpt4 key购买 nike

并非我的所有注释都会显示在我的 map 中,因为我无法使 var annotation = MKPoinatAnnotation 在获取 CKrecords(注释)和获取注释方向时使用相同的 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)
}

最佳答案

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

var points: [MKPointAnnotation] = []

然后填充数组,无论你如何填充它,并通过一个循环运行它,每次迭代都会在 map 上添加一个点:

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

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

这有帮助吗?

关于ios - 如何为注释创建一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47878481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com