Swift Custom Map Call Out Error

144 views Asked by At

I've worked with custom map callouts before and have gotten them to work. Too bad this isn't those situations because I've been trying to figure out why I keep getting this error: "Could not cast value of type 'NSKVONotifying_MKPointAnnotation' (0x1c0115960) to 'ShopPeer.CustomBusinessPoint' (0x101159c68)." I get this error on line "let customAnnotation = view.annotation as! CustomBusinessPoint". I am using this tutorial as an example: http://sweettutos.com/2016/03/16/how-to-completely-customise-your-map-annotations-callout-views/ ... Also, check out in the compiler in the picture below to see how many views appear... not sure why there are so many.

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard !(annotation is MKUserLocation) else { return nil }
    let reuseId = "pin"
    if let pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? CustomBusinessCallOutAnnotatiion {
        return pinView
    } else {
        let  pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView.pinTintColor = UIColor.red
        pinView.canShowCallout = false
        //pinView.rightCalloutAccessoryView = UIButton(type: UIButtonType.infoLight)

        return pinView
    }
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    if view.annotation is MKUserLocation { return }

    let customAnnotation = view.annotation as! CustomBusinessPoint
    let views = Bundle.main.loadNibNamed("CustomBusinessCallOut", owner: nil, options: nil)
    let calloutView = views?[0] as! CustomBusinessCallOut


    calloutView.businessName.text = customAnnotation.businessName


    calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height * -0.0001)
    view.addSubview(calloutView)
    mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}

enter image description here

1

There are 1 answers

0
Mikhail Maslo On

Error is obvious you mismatched something with type. For example, view is not custom or you are not overriding .annotation field in your custom class. You need to look for your class, to make sure view is your superclass. Debug whether view is custom. Finally, read comments under the article you are following - there will be more people who will be possible supporters in your specific question