Chris Comas
Chris Comas

Reputation: 216

Why is my MKAnnotationView not showing?? - Swift

My annotations work, but as soon as I add this code, the annotations stop appearing. What am I doing wrong?

mapview.delegate = self

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

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")
        if annotationView == nil { annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView") }

        if annotation.subtitle == "Innovation" {            annotationView?.backgroundColor = UIColor(named: "Innovation")
        } else if annotation.subtitle == "Art" {            annotationView?.backgroundColor = UIColor(named: "Art")
        } else if annotation.subtitle == "Entertainment" {  annotationView?.backgroundColor = UIColor(named: "Entertainment")
        } else if annotation.subtitle == "Networking" {     annotationView?.backgroundColor = UIColor(named: "Networking")
        } else if annotation.subtitle == "Sale" {           annotationView?.backgroundColor = UIColor(named: "Sale")
        } else if annotation.subtitle == "Sports" {         annotationView?.backgroundColor = UIColor(named: "Sports")
        } else if annotation.subtitle == "Food & Drink" {   annotationView?.backgroundColor = UIColor(named: "Food & Drink")
        } else if annotation.subtitle == "Community" {      annotationView?.backgroundColor = UIColor(named: "Community")
        } else if annotation.subtitle == "Spiritual" {      annotationView?.backgroundColor = UIColor(named: "Spiritual") }
        else { annotationView?.annotation = annotation}

        return annotationView
    }
  let myCoordinate = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
             let mapAnnotation = MKPointAnnotation()
                 mapAnnotation.title = title
                 mapAnnotation.subtitle = color
                 mapAnnotation.coordinate = myCoordinate
                 self.mapView.addAnnotation(mapAnnotation)

** I don't have a custom annotation view, because all I want to do is change the color of the balloon pin **

Upvotes: 1

Views: 642

Answers (1)

Geart Otten
Geart Otten

Reputation: 308

You need to set the delegate of the MapView, and register the annotation to the mapview with the same identifier.

Try

Map.register(MKAnnotationView.self, forAnnotationViewWithReuseIdentifier: "SomeRandomIdentifier")

Hopefully that fixes the problem.

Upvotes: 2

Related Questions