首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKPointAnnotation到MKMapItem

MKPointAnnotation到MKMapItem
EN

Stack Overflow用户
提问于 2019-01-12 10:49:33
回答 1查看 468关注 0票数 1

当我试图打开苹果地图获取方向时,我收到了一个错误“无法将'NSKVONotifying_MKPointAnnotation‘类型的值转换为'MKMapItem'”。我在这方面是新手,我一直在拼凑代码,试图让它工作。

代码语言:javascript
复制
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)
}
EN

回答 1

Stack Overflow用户

发布于 2019-01-12 14:32:39

当您将注释添加到地图中时,您将它们添加为MKPointAnnotation,因此到MKMapItem的强制转换显然会失败。

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

  • view.annotation转换为您的地标类型;
  • 使用该地标创建MKMapItem;以及
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54156378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档