【问题标题】:Custom Apple Map Annotations not working properly自定义 Apple 地图注释无法正常工作
【发布时间】:2020-01-11 13:15:44
【问题描述】:

我要做的是在地图视图 (MKMapView) 上显示自定义注释视图,这在应用程序中运行良好。 问题是当我在应用程序之间切换或在明暗模式之间切换时,自定义注释视图似乎恢复为默认注释引脚,如下所示。

部署目标:iOS 11

运行于:iPhone XS MAX iOS 13.3

Xcode 版本:11.3 (11C29)

Swift 版本:5.0

    private func addAnnotations(_ places : [QPPlaceDM]) {
        mapView.removeAnnotations(myAnnotations)
        var annotations = [MKPointAnnotation]()
        for item in places {
            let annotation = QPCustomAnnotaion()
            annotation.title = item.name
            annotation.coordinate = item.coordinates
            annotation.image = item.category.pinImage
            annotations.append(annotation)
        }
        myAnnotations = annotations
        mapView.addAnnotations(myAnnotations)
    }

.......

extension QPMainVC  : MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        
        if !(annotation is QPCustomAnnotaion) {
            return nil
        }
        let annotationID = "AnnotationId"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationID)

        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationID)
            annotationView?.canShowCallout = true
        }
        else {
            annotationView?.annotation = annotation
        }


        let myCustomAnnotation = annotation as? QPCustomAnnotaion
        annotationView?.image = myCustomAnnotation?.image

        annotationView?.displayPriority = .required

        return annotationView
    }
    
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        guard let annotationLocation = view.annotation?.coordinate else { return }
        var indexOfAnnotation = 0
        for (index , item) in myAnnotations.enumerated() {
            if annotationLocation.latitude == item.coordinate.latitude{
                indexOfAnnotation = index
                break
            }
        }
        let indexPathOfMagorCell = IndexPath(row: indexOfAnnotation, section: 0)
        placesCollection.scrollToItem(at: indexPathOfMagorCell, at: .centeredHorizontally, animated: true)
    }
}

Working properly

After app switching

【问题讨论】:

  • 您是否尝试过在if !(annotation is QPCustomAnnotaion) { return nil } 中设置断点以确保在切换回应用程序时不会调用此行?确定是否是这种情况应该可以帮助您缩小范围。

标签: ios swift xcode mapkit mapkitannotation


【解决方案1】:

在 viewFor 注释函数中从 MKPinAnnotationView 切换到 MKAnnotationView 为我解决了这个问题:

旧:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if !(annotation is CustomMapItemAnnotation) { return nil }
        
        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "id")
        
        if (annotation is CustomMapItemAnnotation) {
            annotationView.canShowCallout = true
            if let placeAnnotation = annotation as? CustomMapItemAnnotation {
                if let type = placeAnnotation.type {
                    print("\(type)-color")
                    annotationView.image = UIImage(named: "\(type)-color")
                }
            }
        }
        return annotationView
    }

新:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if !(annotation is CustomMapItemAnnotation) { return nil }
        
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "id")
        
        if (annotation is CustomMapItemAnnotation) {
            annotationView.canShowCallout = true
            if let placeAnnotation = annotation as? CustomMapItemAnnotation {
                if let type = placeAnnotation.type {
                    print("\(type)-color")
                    annotationView.image = UIImage(named: "\(type)-color")
                }
            }
        }
        return annotationView
    }

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多