【问题标题】:MKMarkerAnnotationView not showing calloutMKMarkerAnnotationView 不显示标注
【发布时间】:2019-11-22 07:14:39
【问题描述】:

我目前正在为通过 MapKit 添加到我的地图视图中的注释创建标注。注释效果很好,但目前没有显示标注,即使我使用正确的代码来启用它们(我相信)。

这里是我的viewFor annotation 代码块。

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

    if annotation is MKUserLocation {
        return nil
    }

    if let annotation = annotation as? ClusterAnnotation {
        let identifier = "cluster"
        return mapView.annotationView(annotation: annotation, reuseIdentifier: identifier)
    } else {
        let identifier = "pin"

        let annotationView = mapView.annotationView(of: MKMarkerAnnotationView.self, annotation: annotation, reuseIdentifier: identifier)
        annotationView.isEnabled = true
        annotationView.canShowCallout = true
        annotationView.accessibilityLabel = "hi"
        annotationView.isHidden = false
        annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        annotationView.markerTintColor = UIColor.customBlue()
        annotationView.glyphImage = UIImage(named: "person")
        return annotationView
    }
}

annotationView 函数的扩展代码块。

extension MKMapView {
    func annotationView<T: MKAnnotationView>(of type: T.Type, annotation: MKAnnotation?, reuseIdentifier: String) -> T {
        guard let annotationView = dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? T else {
            return type.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        }
        annotationView.annotation = annotation
        return annotationView
    }
}

当我选择注释时它会放大,并且在didSelect 代码块中它会运行我通过它运行的打印语句。不完全确定发生了什么导致标注无法显示,即使我已经启用了几乎所有功能。

【问题讨论】:

    标签: ios swift annotations mapkit callouts


    【解决方案1】:

    请使用此代码。 这段代码对我来说很好。 本代码支持 Swift4 和 Swift5。

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
        //  Don't want to show a custom image if the annotation is the user's location.
        if (annotation is MKUserLocation) {
            return nil
        } else {
    
            let annotationIdentifier = "AnnotationIdentifier"
            let nibName = "MyAnnotationView" //My XIB Name
            let viewFromNib = Bundle.main.loadNibNamed(nibName, owner: self, options: nil)?.first as! MyAnnotationView // get My XIB
            var annotationView: MyAnnotationView?
    
            // if there is a view to be dequeued, use it for the annotation
            if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? MyAnnotationView {
    
                if dequeuedAnnotationView.subviews.isEmpty {
                    dequeuedAnnotationView.addSubview(viewFromNib)
                }
                annotationView = dequeuedAnnotationView
                annotationView?.annotation = annotation
            } else {
    
                // if no views to dequeue, create an Annotation View
                let av = MyAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
                av.addSubview(viewFromNib)
                annotationView = av     // extend scope to be able to return at the end of the func
            }
    
            // after we manage to create or dequeue the av, configure it
            if let annotationView = annotationView {
                annotationView.canShowCallout = true                                    // callout bubble
                annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
                annotationView.frame = CGRect(x: 0, y: 0, width: 75, height: 80)
    
                let customView = annotationView.subviews.first as! MyAnnotationView
                customView.frame = annotationView.frame
    
            }
            return annotationView
        }
    }
    

    此代码输出:

    很高兴为您提供帮助。

    【讨论】:

      【解决方案2】:

      来自源代码文档:

          // If YES, a standard callout bubble will be shown when the annotation is selected.
      // The annotation must have a title for the callout to be shown.
      @property (nonatomic) BOOL canShowCallout;
      

      如果没有设置 Title 值,其他项目将不会显示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多