I'm trying to implement a customised annotation view onto a pin in MKMapView. Another user has given me this function to use to generate a customised 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
}
And I would pass the function the parameters of the map I want it on and the annotation I want to customise. i.e. mapView(myMap, viewForAnnotation: point)
This is all well and good and makes sense, however, when I try to add the MKPinAnnotationView to the map using map.addAnnotation(mapView(myMap, viewForAnnotation: point))
it errors saying that the data type is not valid. Does anybody know how to physically render the pin on the map with the customised view?
A simple solution is always best.
Thanks!