2017-03-12 1 views
0

내 응용 프로그램에는 다른 이미지가있는 점을 보여주는지도가 있습니다. 이미지를 정의하는 데 사용되는 일부 매개 변수를 변경하면 표식을 제거하고 추가하여 맵을 업데이트하려고합니다. 문제는 주석을 업데이트하고 다시 추가 할 때 viewFor annotation 메서드는 주석을 nil로받습니다. 나는 무엇을 잘못 할 수 있습니까? 여기Viewforannotation은 내지도를 업데이트 한 후 주석을 nil로 가져옵니다.

내가 변경 한 후 내지도를 업데이트하고하는 것은 : 당신이 고려할 수있는 어떤

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 큐에서 큐로 사용되는 큐로 재사용 가능한 주석으로보기 – breno

답변

0

은 다음과 같습니다 :

func reloadMap(){ 

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

이것은 내가 주석 이미지를 설정하는 데 사용 내 viewForAnnotation 인 경우 보기가 너무 많이 변경되면 다른 reusingIdentifier로 전환하여 대기열을 전환하고 캐시 된보기를 '우회'할 수 있습니다.

관련 문제