2014-09-02 4 views
3

나는 신속 내 MKPointAnnotation에 대한 사용자 정의 "badget"을 만들려고하지만, MKPointAnnotation 이미지와 같은 모든 속성을 가지고 있지 않는 한 실패는 스위프트 MKPointAnnotation 사용자 정의 이미지

var information = MKPointAnnotation() 
    information.coordinate = location 
    information.title = "Test Title!" 
    information.subtitle = "Subtitle" 
    information.image = UIImage(named: "dot.png") //this is the line whats wrong 
    Map.addAnnotation(information) 

사람은 그것을위한 솔루션처럼 신속 알아 냈 ?

+1

'MapKit '을 훨씬 더 자세히 살펴보면'MKPointAnnotation'은 주석 표시를 담당하지 않습니다. 그 기능은'MKAnnotationView'와'MKMapViewDelegate'에 의해 처리됩니다. –

+1

참조 http://stackoverflow.com/questions/24467408/swift-add-mkannotationview-to-mkmapview, http://stackoverflow.com/questions/24523702/stuck -on-using-mkpinannotationview-within-swift-and-mapkit – Anna

답변

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

    if !(annotation is MKPointAnnotation) { 
     return nil 
    } 

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("demo") 
    if annotationView == nil { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "demo") 
     annotationView!.canShowCallout = true 
    } 
    else { 
     annotationView!.annotation = annotation 
    } 

    annotationView!.image = UIImage(named: "image") 

    return annotationView 

} 
+7

설명을 추가하십시오. – d4Rk