2016-12-02 3 views
0

2 개의 주석이있는지도가 있으며 각각에 2 개의 다른 이미지가 필요합니다.Swift를 사용하여 이미지를 MKAnnotation으로 변경하는 방법

하나의 주석을 사용하여이를 수행하는 방법을 알고 있지만 두 가지 주석이 있습니다.

여기에 하나 내 코드입니다 :

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     let reuseId = "pin" 

     var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 


     anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     anView!.image = nil; 
     anView!.image = UIImage(named:"destinationPin") 


     return anView 

    } 

그리고 당신은 reuseId 무엇인지 설명해 경우 누군가가 정말 고맙겠 할 수있는 경우에

. 지도에 주석을 추가 할 때 사전

+0

두 개의 특수 효과를 의미합니까? 2 개의 별도 강의? – sdasdadas

+0

지도에 두 개의 핀이 있는데 각기 서로 다른 이미지를 갖고 싶습니다. –

답변

1

최초의

덕분에, 당신은 그들을 구별해야합니다. 쉬운 방법은 태그 값을 설정하는 것입니다. 당신의 논리를 분기하는 데 사용할 수 있습니다

if annotation.tag == 0 { 
    anView!.image = UIImage(named:"destinationPin") 
} else { 
    anView!.image = UIImage(named:"alternateDestinationPin") 
} 

참고 MKAnnotation 제목과 같은 다른 특성을 가지고 있으며, 그 좌표 : 그럼 당신은 다음과 같이 당신에게 논리를 분기 할 수 있습니다.

+0

답변을 주셔서 감사합니다. 최대한 빨리 확인하고 알려 드리겠습니다. –

+0

제목을 사용했습니다. 나는 당신이 일반 무언가로 꼬리표를 붙인다고 생각하니? (주석은 회원 태그가 없다는 오류를 반환했기 때문에) –

+0

예. 내 현재 프로젝트에서 나는 adasdadas처럼 서브 클래 싱을 제안하고 태그 값을 추가했습니다. 나는 대답을 게시했을 때 잊어 버렸다. –

0

옵션으로 캐스팅 할 수 있습니다.

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    let reuseId = "pin" 

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 

    if let annotation1 = anView as? FirstAnnotation { 
     annotation1.image = UIImage(named: "first.jpg") 
    } else if let annotation2 = anView as? SecondAnnotation { 
     annotation2.image = UIImage(named: "second.jpg") 
    } 
    return anView 
} 

이 추가 된 타입 안전의 이익과 tag 내부 거짓말 무엇을 자신의 지식에 의존하지 있습니다. 두 개의 개별 MKAnnotation 유형이있는 경우 실제로 두 개의 개별 클래스로 하위 클래스해야합니다.

관련 문제