更改图像引脚图

时间:2015-10-17 17:37:24

标签: ios mkmapview swift2

为了拍摄个人照片而不是传统的红色针脚,我应该写什么?

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

    if annotation is MKUserLocation {
        return nil
    }

    let annView : MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "currentloc")
    annView.pinTintColor = UIColor.redColor()
    annView.animatesDrop = true
    annView.canShowCallout = true
    annView.calloutOffset = CGPointMake(-8, 0)

    annView.autoresizesSubviews = true
    annView.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIView

    return annView
}

1 个答案:

答案 0 :(得分:2)

使用MKAnnotationView代替MKPinAnnotationView,然后设置其image属性。我还建议实现dequeue逻辑,以便可以重用注释:

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

    let annotationIdentifier = "SomeCustomIdentifier" // use something unique that functionally identifies the type of pin

    var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier)

    if annotationView != nil {
        annotationView.annotation = annotation
    } else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)

        annotationView.image = UIImage(named: "annotation.png")

        annotationView.canShowCallout = true
        annotationView.calloutOffset = CGPointMake(-8, 0)

        annotationView.autoresizesSubviews = true
        annotationView.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIView
    }

    return annotationView
}