UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.