Search code examples
iosswiftannotationsmkmapviewcustomization

Layout for class


I have a problem regarding the link between two blocks of code which will hopefully change the annotation image. I am new to swift, So I am having a hard time trying to figure it on a mapView. I've given a code in the function for print a string when it runs, but the string is not showing in the NSLog. Does this mean that, the function is not being run at all? I would appreciate any suggestions for where I went wrong. Thank you

class ViewController2: UIViewController {

@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()

//Location for first Pin
let locationOne = CLLocationCoordinate2DMake(-47.016945, 167.852095)

//Location for map centering
let locationNZ = CLLocationCoordinate2DMake(-43.937462, 170.507813)
let span = MKCoordinateSpanMake(9, 9)
let region = MKCoordinateRegion(center: locationNZ, span: span)
mapView.setRegion(region, animated: true)

//Create annotation one
let annotation = MKPointAnnotation()
annotation.coordinate = locationOne
annotation.subtitle = "Park"
annotation.title = "Rakiura National Park"

//Add annotation to the map
mapView.addAnnotation(annotation)}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()}




func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

print("func")
guard !annotation.isKind(of: MKUserLocation.self) else {
return nil}

let annotationIdentifier = "AnnotationIdentifier"

var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: 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 {
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "mapIcon.png")}

return annotationView}}

Solution

  • It sounds like you may have the I forgot to set the delegate so my delegate never got called problem; which should always be what you suspect when your delegate method doesn't fire. Try

    mapView.delegate = self