You need to implement the viewForAnnotation
delegate method and return an MKAnnotationView
(or subclass) from there.
This is just like in Objective-C -- the underlying SDK works the same way.
Remove the creation of MKPinAnnotationView
from the for
loop that adds the annotations and implement the delegate method instead.
Here is a sample implementation of the viewForAnnotation
delegate method in Swift:
func mapView(mapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .Purple
}
else {
pinView!.annotation = annotation
}
return pinView
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…