2017-10-29 2 views
2

나는 (내가 MKAnnotationView의 서브 클래스로 생성) 사용자 정의 주석 뷰를 등록하기 위해 노력하고있어,하지만 난 그 몇 가지 문제가 있습니다. 처음에, 나는 아래의 코드를 사용하여 하나의 주석 마커에 대해 하나의 사용자 정의 주석 뷰를 등록하려고했습니다 (RestaurantMarkerView 내 사용자 지정 주석이다) : 나는 핀을 누를 때두 개 이상의 사용자 정의 주석 뷰를 등록하는 방법은 무엇입니까?

mapView.register(RestaurantMarkerView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier) 

그것은 잘 작동은 (는 보여줍니다 콜 아웃). 지금은 다른 주석 마커를 추가하고 또 다른 뷰를 등록하려고하고 내가 아는 한,이 경우 I의 의견에 대한 사용자 정의 식별자를 사용한다, 그래서 나는이 코드에 의해 그렇게 : 이제

mapView.register(RestaurantMarkerView.self, forAnnotationViewWithReuseIdentifier: "a") 
    mapView.register(ChoosenRestaurantMarkerView.self, forAnnotationViewWithReuseIdentifier: "b") 

마커를 탭하면 콜 아웃이 표시되지 않습니다. 마커 중 하나의 재사용 식별자를 MKMapViewDefaultAnnotationViewReuseIdentifier으로 변경하면 해당 마커가 콜 아웃을 표시합니다. 그렇다면 어떻게 여러 개의 사용자 정의 주석 뷰를 등록 할 수 있습니까? 감사.

+2

당신이 (당신이 다른보기를 원하기 때문에이 경우 수 없습니다) 다음 (은'MKMapViewDelegate' 기능'지도보기를 구현해야합니다 표준 주석보기 재사용 식별자를 사용하지 않을 경우 _ : viewFor :)'적절한보기를 반환하려면 – Paulw11

+0

@ Paulw11 대단히 감사합니다! –

답변

2

@Paulw은 MKMapViewDelegate 함수 mapView(_:viewFor:)을 구현하여 적절한보기를 반환해야한다고 말합니다.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKUserLocation { 
     return nil 
    } 

    if /* condition */ { 
     let reuseId = "a" 
     var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) 
     if pinView == nil { 
      pinView = RestaurantMarkerView(annotation: annotation, reuseIdentifier: reuseId) 
      pinView?.canShowCallout = true 
     } else { 
      pinView?.annotation = annotation 
     } 

     return pinView 
    } else { 
     let reuseId = "b" 
     var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) 
     if pinView == nil { 
      pinView = ChoosenRestaurantMarkerView(annotation: annotation, reuseIdentifier: reuseId) 
      pinView?.canShowCallout = true 
     } else { 
      pinView?.annotation = annotation 
     } 

     return pinView 
    } 
} 
+0

고맙습니다. 그래서 매번 하나 이상의 커스텀 어노테이션 뷰가있을 때마다이 메소드를 사용해야한다. 맞습니까? –

+0

예. 하지만이 경우 스위치 문은 if 블록의 계단식 집합보다 읽기 쉽다고 생각합니다. –

+0

예, 물론입니다. 도와 줘서 고마워. –

관련 문제