设置MKMAPVIEW的注释视图图像显示错误的图像



i具有以下代码来设置销钉图像,用于创建数组中的对象。问题在于,引脚没有显示针对销的正确图像。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let identifier = "MyPin"
        if annotation is MKUserLocation {
            return nil
        }
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView?.canShowCallout = true
            var selectedStation = fuel()
            for station in fuelStations {
                if (annotation.title! == station.Name){
                    selectedStation = station
                }
            }
            var imageName = ""
            switch(selectedStation.Brand) {
            case "MORRISONS"  :
                imageName = "fuel-morrisons"
                break;
            case "TEXACO"  :
                imageName = "fuel-texaco"
                break;
            case "SAINSBURY"  :
                imageName = "fuel-sainsbury"
                break;
            case "SHELL"  :
                imageName = "fuel-shell"
                break;
            case "BP"  :
                imageName = "fuel-bp"
                break;
            case "TESCO"  :
                imageName = "fuel-tesco"
                break;
            case "TESCO EXTRA"  :
                imageName = "fuel-tesco"
                break;
            case "ESSO"  :
                imageName = "fuel-esso"
                break;
            default : imageName = "NEWFuel"
            }
            print("selected station: " + selectedStation.Brand + " imagename: " + imageName)
            let pinImage = UIImage(named: imageName)
            let size = CGSize(width: 40, height: 45)
            UIGraphicsBeginImageContext(size)
            pinImage!.draw(in: CGRect(x:0, y:0, width:size.width, height:size.height))
            let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            annotationView?.image = resizedImage
        } else {
            annotationView?.annotation = annotation
        }
        return annotationView
    }

这些引脚由以下方式创建:

for station in self.fuelStations{
                        let myAnnotation: MKPointAnnotation = MKPointAnnotation()
                        myAnnotation.coordinate = CLLocationCoordinate2DMake(station.lat, station.lng);
                        myAnnotation.title = station.Name
                        myAnnotation.subtitle = station.Brand
                        DispatchQueue.main.async {
                            self.mapView.addAnnotation(myAnnotation)
                        }
                    }

打印建议的淘汰应该正确起作用,错误的图像仍然在错误的引脚上。

selected station: TOTAL imagename: NEWFuel
selected station: TESCO EXTRA imagename: fuel-tesco
selected station: BP imagename: fuel-bp
selected station: ESSO imagename: fuel-esso
selected station: GULF imagename: NEWFuel
selected station: ESSO imagename: fuel-esso
selected station: ESSO imagename: fuel-esso
selected station: TESCO EXTRA imagename: fuel-tesco
selected station: MURCO imagename: NEWFuel
selected station: BP imagename: fuel-bp
selected station: GULF imagename: NEWFuel
selected station: JET imagename: NEWFuel
selected station: ESSO imagename: fuel-esso
selected station: GULF imagename: NEWFuel
selected station: ASDA imagename: NEWFuel
selected station: BP imagename: fuel-bp
selected station: TESCO imagename: fuel-tesco
selected station: JET imagename: NEWFuel
selected station: SHELL imagename: fuel-shell
selected station: ESSO imagename: fuel-esso
selected station: ESSO imagename: fuel-esso
selected station: SHELL imagename: fuel-shell
selected station: ASDA imagename: NEWFuel
selected station: SHELL imagename: fuel-shell
selected station: BP imagename: fuel-bp
selected station: ESSO imagename: fuel-esso
selected station: ESSO imagename: fuel-esso
selected station: TOTAL imagename: NEWFuel

您的图像选择逻辑是错误的(位于错误的位置)。您需要对到达的每个进行图像选择逻辑,而不仅仅是未重复使用的新逻辑。

这就是您正在做的:

    if annotationView == nil {
        // your image-choosing logic
    } else {
        annotationView?.annotation = annotation
    }

因此,如果您的注释视图是重复使用的注释,您什么也不做;您的逻辑从未使用过。但这意味着您不确定哪个图像会在这里进行。但是有一个图像:旧的,重复使用的注释视图中的图像。因此,这是此注释的错误图像。