Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义Apple地图批注无法正常工作_Ios_Swift_Xcode_Mapkit_Mapkitannotation - Fatal编程技术网

Ios 自定义Apple地图批注无法正常工作

Ios 自定义Apple地图批注无法正常工作,ios,swift,xcode,mapkit,mapkitannotation,Ios,Swift,Xcode,Mapkit,Mapkitannotation,我试图做的是在地图视图(MKMapView)上显示一个自定义注释视图,这在应用程序中运行良好。 问题是,当我在应用程序之间切换或在黑暗和光明模式之间切换时,自定义注释视图似乎恢复为默认注释pin,如下所示 部署目标:iOS 11 运行于:iPhone XS MAX iOS 13.3 Xcode版本:11.3(11C29) Swift版本:5.0 private func addAnnotations(_ places : [QPPlaceDM]) { mapView.re

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

部署目标: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)
    }
}


在viewFor annotation func中从MKPinAnnotationView切换到MKAnnotationView为我解决了这个问题:

旧的:

新的:


您是否尝试过在
if!中设置断点!(注释是QPCustomAnnotation){return nil}
以确保在切换回应用程序时不会调用此行?确定情况是否如此应该有助于缩小范围。
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
    }