更改图像图钉图

change image pin maps

我应该写什么才能放个人照片而不是传统的红色别针?

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
}

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

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
}