我正在尝试将自定义注释视图应用到MKMapView中的标记上。另一个用户给了我这个函数,用于生成自定义的MKPinAnnotationView。https://stackoverflow.com/users/3845091/zisoft
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is PinAnnotation {
let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")
pinAnnotationView.pinColor = .Purple
pinAnnotationView.draggable = true
pinAnnotationView.canShowCallout = true
pinAnnotationView.animatesDrop = true
let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
deleteButton.frame.size.width = 44
deleteButton.frame.size.height = 44
deleteButton.backgroundColor = UIColor.redColor()
deleteButton.setImage(UIImage(named: "trash"), forState: .Normal)
pinAnnotationView.leftCalloutAccessoryView = deleteButton
return pinAnnotationView
}
return nil
我需要将函数的参数设置为我想要的地图和注释的自定义视图。例如:mapView(myMap, viewForAnnotation: point)
这很好理解,但是当我尝试使用 map.addAnnotation(mapView(myMap, viewForAnnotation: point))
将MKPinAnnotationView添加到地图时,会出现数据类型无效的错误。有人知道如何在地图上实际渲染带有自定义视图的标注吗?
最好的解决方案是简单明了。
谢谢!