首页 > 解决方案 > 为什么我的 MKAnnotationView 没有显示?- 斯威夫特

问题描述

我的注释工作,但只要我添加此代码,注释就会停止出现。我究竟做错了什么?

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)

** 我没有自定义注释视图,因为我想做的只是改变气球别针的颜色 **

标签: swiftannotationsmkmapview

解决方案


您需要设置 MapView 的委托,并将注解注册到具有相同标识符的地图视图。

尝试

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

希望能解决问题。


推荐阅读