gpt4 book ai didi

swift - MKPointAnnotation 到 MKMapItem

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

尝试打开 Apple map 获取路线时,出现错误“无法将‘NSKVONotifying_MKPointAnnotation’类型的值转换为‘MKMapItem’”。我是这方面的新手,我一直在拼凑代码以尝试使其工作。

struct Location {
let agencyId: String
let agencyEventId: String
let agencyEventSubTypeCode: String
let latitude: Double
let longitude: Double
let agencyEventTypeCode: String
}

func multiPoint() {

for receivedEvent in receivedEventsList {
mapEventLatitude = receivedEvent.latitude!
mapEventLongitude = receivedEvent.longitude!
latDouble = ("\(mapEventLatitude))" as NSString).doubleValue
longDouble = ("\(mapEventLongitude))" as NSString).doubleValue
multiMapAgencyEventSubTypeCode = receivedEvent.agencyEventSubtypeCode!
multiMapAgencyId = receivedEvent.agencyId!
multiMapAgencyEventId = receivedEvent.agencyEventId!
multiMapAgencyEventTypeCode = receivedEvent.agencyEventTypeCode!

let locations = [
Location(agencyId: multiMapAgencyId, agencyEventId: multiMapAgencyEventId, agencyEventSubTypeCode: multiMapAgencyEventSubTypeCode, latitude: latDouble, longitude: longDouble, agencyEventTypeCode: multiMapAgencyEventTypeCode)
]


for location in locations {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
annotation.title = location.agencyId
annotation.subtitle = multiMapAgencyEventSubTypeCode
multiEventMap?.addAnnotation(annotation)
eventTypeNumber = ("\(multiMapAgencyEventTypeCode))" as NSString).intValue
}
}
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let reuseIdentifier = "annotationView"
let view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
view.markerTintColor = UIColor.blue
view.glyphText = "x"
view.displayPriority = .required
view.clusteringIdentifier = nil
view.canShowCallout = true
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
return view
}

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let location = view.annotation as! MKMapItem
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
location.openInMaps(launchOptions: launchOptions)
}

最佳答案

当您将注释添加到 map 时,您将它们添加为 MKPointAnnotation,因此转换为 MKMapItem 显然会失败。

我建议不要创建 MKPointAnnotation,而是创建一个 MKPlacemark 或创建您自己的包含附加属性的 MKPlacemark 子类你感兴趣的。然后你的calloutAccessoryControlTapped可以

  • view.annotation 转换为您的地标类型;
  • 使用该地标创建一个 MKMapItem;然后
  • mapItem.openInMaps(launchOptions:)

关于swift - MKPointAnnotation 到 MKMapItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54156378/

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