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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.