I\'m trying to change from Swift 1.2 to Swift 2.0 and I\'m at the end of the changes. Currently I\'m making changes in the MapViewController, and there isn\'t any error or w
Here is the answer:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "custom_pin.png")
annotationView.rightCalloutAccessoryView = detailButton
}
else {
annotationView.annotation = annotation
}
return annotationView
}
Regards
The accepted answer doesn't work, as it has annotationView
uninitialized in else
block.
Here's a better solution. It dequeues annotation view if possible or creates a new one if not:
Swift 3, 4:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !(annotation is MKUserLocation) else {
return nil
}
// Better to make this class property
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImage")
}
return annotationView
}
Swift 2.2:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !annotation.isKindOfClass(MKUserLocation) else {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImage")
}
return annotationView
}
SWIFT 3,4
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
if (pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
}
pinView?.canShowCallout = false
return pinView
}