2014-09-12 1 views
2

이미지에 버스 정류장을 나타내는 일부 핀을 놓으려고합니다. 이미지가 핀 배치를 변경하면 광고가 표시됩니다. 이미지를 설정하지 않으면 핀이 올바른 위치에 떨어집니다.MKAnnotationView 치환 핀에 이미지 추가

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 

    if annotation is StopAnnotation { 

     let identifier = "stopAnnotation" 
     var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 
     if pinView == nil { 
      //println("Pinview was nil") 
      pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      pinView!.canShowCallout = true 
      pinView.image = UIImage(named: "stopIcon") 
     } 
     return pinView 

    } 
    return nil 
} 

enter image description here enter image description here

내가 사용하려고 이미지 : 가 enter image description here

이 사람이이 일을하는 이유를 말씀해 주시겠습니까? 이 앱의 Obj-C 버전에서 똑같은 이미지를 사용하고 있으며 모든 것이 잘 작동합니다.

답변

5

코드는 사용자 지정 이미지로 MKPinAnnotationView을 생성합니다.

MKPinAnnotationView 클래스는 기본 핀 이미지를 표시하는 데에만 사용해야합니다.

맞춤 이미지를 표시하려면 일반 MKAnnotationView을 사용하는 것이 좋습니다.


코드가 MKPinAnnotationView를 사용하고 있기 때문에

, 화상은 자동으로 그것에합니다 ( centerOffset 속성) 오프셋인가 받고있다.

이 기본 제공 오프셋은 기본 핀 이미지에서 작동하지만 사용자 정의 이미지에서는 작동하지 않습니다.

오히려이 기본 동작을 재정의하는 것보다, 대신 일반 MKAnnotationView 사용

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 

    if annotation is StopAnnotation { 

     let identifier = "stopAnnotation" 
     var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 
     if pinView == nil { 
      //println("Pinview was nil") 

      //Create a plain MKAnnotationView if using a custom image... 
      pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 

      pinView!.canShowCallout = true 
      pinView.image = UIImage(named: "stopIcon") 
     } 
     else { 
      //Unrelated to the image problem but... 
      //Update the annotation reference if re-using a view... 
      pinView.annotation = annotation 
     } 

     return pinView   
    } 
    return nil 
} 
관련 문제