1

MKPinAnnotationView在我的应用程序中使用。

我将MapView对象设置为委托,并使用此代码自定义我的AnnotationView

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

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
           // pinView!.canShowCallout = true
            pinView!.image = UIImage(named:"store.jpg")
            pinView!.animatesDrop = true
            pinView!.pinTintColor = UIColor.darkGrayColor()
         }
        else {
            pinView!.annotation = annotation
        }

        return pinView
    }

我正在根据需要获取自定义 AnnotationView。但是,我缺少titlesubtitle的功能MKPointAnnotation

我希望看到灰点的标题和副标题。在此处输入图像描述

4

1 回答 1

1

我正在覆盖一个功能

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
        mapView.deselectAnnotation(view.annotation, animated: true)
    }

我把这个函数注释掉了,得到了标题和字幕。

更新代码

/*
 func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
 mapView.deselectAnnotation(view.annotation, animated: true)
 }
 */
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"
    let  pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    pinView.canShowCallout = true
    pinView.animatesDrop = true
    pinView.pinTintColor = UIColor.darkGrayColor()
    pinView.draggable = true
    pinView.accessibilityLabel = "hello"
    let btn = UIButton(type: .DetailDisclosure)
    pinView.rightCalloutAccessoryView = btn
    return pinView
}
于 2016-06-23T06:37:19.467 回答