2017-03-12 27 views
0

在我的應用程序中,我有一張地圖,顯示您的點與一些不同的圖像。當我對用於定義圖像的某些參數進行一些更改時,我嘗試通過刪除和添加記號來更新地圖。問題是,當我更新註釋並再次添加它們時,方法viewFor註解接收註釋爲nil。我能做什麼錯了?Viewforannotation在更新我的地圖後獲取註釋爲零

我在這裏進行更改後,更新我的地圖:

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 
    } 
+0

http://stackoverflow.com/questions/25591038/is-there-any-m ethod到復位的隊列使用的功能於dequeuereusableannotationviewwith – breno

回答

0

可以考慮的是:如果視圖會改變太多,你可以切換到一個不同的reusingIdentifier,從而切換隊列和'規避'緩存的視圖