【发布时间】:2017-11-02 23:10:35
【问题描述】:
我之前使用过自定义地图标注,并且已经让它们发挥作用。太糟糕了,这不是那些情况,因为我一直在试图弄清楚为什么我不断收到此错误:“无法将 'NSKVONotifying_MKPointAnnotation' (0x1c0115960) 类型的值转换为 'ShopPeer.CustomBusinessPoint' (0x101159c68)。”我在“让 customAnnotation = view.annotation as!CustomBusinessPoint”这一行收到此错误。我以本教程为例:http://sweettutos.com/2016/03/16/how-to-completely-customise-your-map-annotations-callout-views/ ...另外,在下图中的编译器中查看出现了多少视图...不知道为什么会有这么多。
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)
}
【问题讨论】:
标签: ios swift mapkit mapkitannotation callout