How to display MKPinAnnotationView on MKMapview in swift
I am trying to implement custom output annotation view in MKMapView. Another user gave me this function to create a custom 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 will pass to the function the parameters of the map I want and the annotation I want to customize. those.mapView(myMap, viewForAnnotation: point)
This is good, good and makes sense, however, when I try to add an MKPinAnnotationView to the map using map.addAnnotation(mapView(myMap, viewForAnnotation: point))
, there are errors in it saying that the datatype is invalid. Does anyone know how to physically render the output on a map using a customized view?
A simple solution is always better.
Thank!
source to share
In your question, you say about the method viewForAnnotation
:
And I will pass the function to the parameters of the map in which I want it and the annotation that I want to customize. those. mapView (myMap, viewForAnnotation: point)
No, itβs wrong.
You didn't pass parameters to the function, etc.
This viewForAnnotation
method is to delegate to a method that you implement, but not that is called directly by you.
Showing maps ( MKMapView
) will cause it to function and pass parameters, etc.
The delegate method viewForAnnotation
is where you can provide a custom view for a given model annotation object .
To ensure that the Map View invokes your delegation methods, you must either:
- Make sure the map output is
delegate
connected to your own view controller in the storyboard, or - In code, usually in
viewDidLoad
, domapView.delegate = self
.
When you call
addAnnotation
, you pass it a model annotation object (for example,
MKPointAnnotation
or anything that implements the protocol
MKAnnotation
).
So this line:
map.addAnnotation(mapView(myMap, viewForAnnotation: point))
should look something like this:
map.addAnnotation(point)
An object MKPinAnnotationView
(and everything that subclasses MKAnnotationView
) is a representation for some object of an annotation model - they are not the same thing. p>
These questions and topics have been covered many times in the iOS and MapKit documentation, on SO, and in tutorials available elsewhere.
Here are some SO related questions that you might find useful:
source to share