3

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!

Community
  • 1
  • 1
Matt Spoon
  • 2,760
  • 5
  • 25
  • 41

1 Answers1

2

In your question, you say about the viewForAnnotation method:

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)

No, this is wrong.

You would not pass the function the parameters, etc.

That viewForAnnotation method is a delegate method you implement but is not called directly by you.

The map view (MKMapView) will call it and pass the function the parameters, etc.

The viewForAnnotation delegate method is where you can provide a custom view for the given annotation model object.

To make sure the map view calls your delegate methods, you must either:

  • Make sure the map view's delegate outlet is connected to its view controller in the storyboard, or
  • In code, usually in viewDidLoad, do mapView.delegate = self.


When you call addAnnotation, you pass it the annotation model object (eg. an MKPointAnnotation or anything that implements the MKAnnotation protocol).

So this line:

map.addAnnotation(mapView(myMap, viewForAnnotation: point))

should be something like:

map.addAnnotation(point)

An MKPinAnnotationView object (and anything that subclasses MKAnnotationView) is the view for some annotation model object -- they are not the same things.


These questions and topics have been covered already many times in the iOS and MapKit documentation, on SO and on tutorials available elsewhere.

Here are just a couple of related questions on SO that you may find useful:

Community
  • 1
  • 1