更新我的地图后,Viewforannotation会将注释标记为nil

时间:2017-03-12 17:15:12

标签: ios swift mapkit

在我的应用程序中,我有一张地图,显示您的点与一些不同的图像。当我在用于定义图像的某个参数中进行一些更改时,我尝试通过删除和添加符号来更新地图。问题是,当我更新注释并再次添加它们时,方法viewFor annotation接收注释为nil。我能做错什么?

这里我在进行更改后更新我的地图:

func reloadMap(){

        for annotation in mapView.annotations {
            let a = annotation
            self.mapView.removeAnnotation(a)
            DispatchQueue.main.async {
                self.mapView.addAnnotation(a)
            }
        }
    }

这是我用来设置注释图像的viewForAnnotation:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        let annotationIdentifier = "pin"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView?.canShowCallout = true

            var categoryName = ""
            var colorName = ""
            // Resize image
            if annotation is MyAnnotation{
                let index = (annotation as! MyAnnotation).annotationIndex

                let annotationData = searchResult[index]
                let categories = annotationData.object(forKey: "category") as! NSArray
                for item in categories {
                    let category = item as! String
                    if hasCategory(category: category) {
                        categoryName = translateCategoryName(category: category)
                        break
                    }
                }

                let status = annotationData.object(forKey: "status") as! String


                if status == "Lotado"{
                    colorName = "vermelho"

                } else if status == "Quase cheio"{
                    colorName = "laranja"

                } else if status == "Médio"{
                    colorName = "amarelo"

                }else if status == "Vazio"{
                    colorName = "verde"
                } else {
                    colorName = "cinza"
                }
            }

            var imageName = ""

            if categoryName != "" {
                imageName = "\(colorName)-\(categoryName)"
            } else {
                imageName = "verde-padaria"
            }

            if annotation is MKUserLocation {
                let userAvatar = defaults.integer(forKey: "avatarNumber")
                imageName = "balao-avatar\(userAvatar)"
            }

            let pinImage = UIImage(named: imageName)
            let size = CGSize(width: 30, height: 50)
            UIGraphicsBeginImageContext(size)
            pinImage?.draw(in: CGRect.init(x: 0.0, y: 0.0, width: size.width, height: size.height))
            let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            annotationView?.image = resizedImage

            if !(annotation is MKUserLocation) {
                let btn = UIButton(type: .infoLight)
                btn.addTarget(self,action:#selector(showEstablishment),for:.touchUpInside)
                annotationView?.rightCalloutAccessoryView = btn
            }

            return annotationView
        }
        else {
            annotationView?.annotation = annotation
        }

        return annotationView
    } 

1 个答案:

答案 0 :(得分:0)

您可以考虑的是:如果视图变化太大,您可以切换到不同的reusingIdentifier,从而切换队列并“绕过”缓存的视图