自定义的苹果地图标注无法正常工作。

3
我试图在地图视图(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)
    }
}

正常工作

应用程序切换后


你尝试在 if !(annotation is QPCustomAnnotaion) { return nil } 中设置断点,以确保在切换回应用程序时不会调用此行吗?确定是否是这种情况应该有助于缩小问题范围。 - David Chopin
1个回答

1
切换视图函数中的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
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接