2017-10-25 15 views
0

Map에서 init 메소드로 일부 annotationViews를 추가했습니다 (id가 초기화 됨). 이제 탐색 바에서 클릭 버튼으로 특정 ID 주석보기를 업데이트하려고합니다. I는 IDS 5 주석 첨가했다고 가정NavigationBar에서 클릭 버튼으로 특정 MKAnnotationView 이미지를 업데이트하는 방법은 무엇입니까?

(1, 2, 3, 4, 5) VC에서

라이트 :

let annotation = MapPinAnnotation(title: storeItem.name!, location: CLLocationCoordinate2DMake(Double(lat), Double(long)), id: storeItem.storeId!) 
self.mapview.addAnnotation(annotation) 

초기화 AnnotationView :

class MapPinAnnotation: NSObject, MKAnnotation { 

    var title:String? 
    var id:String? 
    private(set) var coordinate = CLLocationCoordinate2D() 

    init(title newTitle: String, location: CLLocationCoordinate2D, id: String) { 
     super.init() 

     self.title = newTitle 
     self.coordinate = location 
     self.id = id 
    } 
} 

ViewFor 주석 방법 :

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     if (annotation is MKUserLocation) { 
      return nil 
     } 
     if (annotation is MapPinAnnotation) { 
      let pinLocation = annotation as? MapPinAnnotation 
      // Try to dequeue an existing pin view first. 
      var annotationView: MKAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: "MapPinAnnotationView") 
      if annotationView == nil { 
       annotationView?.image = UIImage(named: Constants.Assets.PinGreen) 
      } 
      else { 
       annotationView?.annotation = annotation 
      } 
      return annotationView 
     } 
     return nil 
    } 

이제 네비게이션 바에서 클릭 버튼으로 어노테이션 뷰 (id 4)의 이미지를 변경하고 싶습니다.

어떻게 업데이트 할 수 있습니까? 도와주세요. 미리 감사드립니다.

답변

1

view(for:) 방법으로 특정 MKAnnotationView를 가져올 수 있습니다. 다음 코드를 사용해보십시오.

func clickButton() { 
    for annotation in self.mapView.annotations { 
     if annotation.id == 4 { 
      let annotationView = self.mapView.view(for: annotation) 
      annotationView?.image = UIImage(named: "Image name here") 
     } 
    } 
}