1

I'm getting an error "Could not cast value of type 'NSKVONotifying_MKPointAnnotation' to 'MKMapItem'" when trying to open Apple Maps for directions. I'm new at this and I've been piecing together code to try to make it work.

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)
}
jvan
  • 173
  • 1
  • 9

1 Answers1

0

When you add your annotations to your map, you added them as MKPointAnnotation, so the cast to MKMapItem will obviously fail.

I’d suggest that instead of creating MKPointAnnotation, create either a MKPlacemark or create your own subclass of MKPlacemark that includes the additional properties that you’re interested in. Then your calloutAccessoryControlTapped can

  • cast the view.annotation to your placemark type;
  • create a MKMapItem using that placemark; and then
  • mapItem.openInMaps(launchOptions:)
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks for the reply. As stated, I’m very new to developing so I’ll be trying to figure out how to piece that together. – jvan Jan 14 '19 at 01:03