BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: Project16 Day 60: Map pins not showing

Forums > 100 Days of Swift

I have completed Project16 by following the instructions as far as I can tell, but in the end it isn't showing the map pins at all anymore.

When I add this function as instructed here, it causes the map pins to stop showing. But if I delete or comment out this function then it shows the map pins, just without annotation information available.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard annotation is Capital else { return nil }
        let identifier = "Capital"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)        

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView?.canShowCallout = true

            let btn = UIButton(type: .detailDisclosure)
            annotationView?.rightCalloutAccessoryView = btn
        } else {
            annotationView?.annotation = annotation
        }

        return annotationView
}

I've looked over this function a few times now and can't seem to find any differences between this and what Paul gave us in the instructions. Does anybody know how to make this work?

And yes, I have made my ViewController the Delegate for the MKMapView and made it conform to MKMapViewDelegate as instructed.

3      

Nevermind, I found the problem. I was using the initializer for MKAnnotationView(annotation:, reuseIdentifier:) instead of MKPinAnnotationView(annotation:, reuseIdentifier:)

However, after trying to use that, I found that it was depricated, so I ended up using MKMarkerAnnotationView(annotation:, reuseIdentifier:) instead, and now it is working as expected.

3      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.