ios - 为显示错误图像的 MKMapView 设置 annotationView 图像

标签 ios swift

我有下面的代码来设置图钉图像,基于数组中的哪个对象用于创建图钉。 问题是图钉未显示图钉的正确图像。

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
    }

因此,如果您的注释 View 是一个重用 注释,您什么都不做;你的逻辑从未被使用过。但这意味着您无法确定此处放的是什么图像。但是有一张图片:来自旧的、重复使用的注释 View 的图片。因此,该注释的图像是错误的。

关于ios - 为显示错误图像的 MKMapView 设置 annotationView 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47611848/

相关文章:

ios - 弹出到 Root View Controller

ios - queryPedometerData 返回 nil 步数,即使记录了步数

ios - 快速计算大文件的 MD5 校验和

arrays - 类型 'Bool' 不符合协议(protocol) 'Sequence'

c++ - 调用 [[NSBundle mainBundle] bundlePath] 时是否有生成相同字符串的 CoreFoundation 等效项?

ios - iOS:从音频文件获取原始PCM数据

swift - Swift 中的斐波那契单线

ios - Swift,检查数组是否在索引处有值

xcode - 如何在 Xcode 中只更改应用程序名称?

ios - 在方向变化时以编程方式重绘 subview