MKPointAnnotation 到 MKMapItem
Posted
技术标签:
【中文标题】MKPointAnnotation 到 MKMapItem【英文标题】:MKPointAnnotation to MKMapItem 【发布时间】:2019-01-12 02:49:32 【问题描述】:我在尝试打开 Apple 地图获取路线时收到错误消息“无法将 '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)
【问题讨论】:
【参考方案1】:当您将注释添加到地图时,您将它们添加为MKPointAnnotation
,因此转换为MKMapItem
显然会失败。
我建议不要创建MKPointAnnotation
,而是创建MKPlacemark
或创建您自己的MKPlacemark
子类,其中包含您感兴趣的其他属性。然后您的calloutAccessoryControlTapped
可以
view.annotation
转换为您的地标类型;
使用该地标创建MKMapItem
;然后
mapItem.openInMaps(launchOptions:)
【讨论】:
感谢您的回复。如前所述,我对开发非常陌生,因此我将尝试弄清楚如何将它们组合在一起。以上是关于MKPointAnnotation 到 MKMapItem的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 MKPointAnnotation 不是自定义的?
MKPointAnnotation 不再在 iOS 11 中显示标题