注释标注不起作用 Swift

Annotation callout not working Swift

我想在 MapKit 中为我的注释添加一个标注,但在网上搜索了很多之后我无法让它工作。

这就是我目前创建每个注释的方式,目前效果很好。

struct loc {
    let title: String
    let latitude: Double
    let longitude: Double
    let phone: Int
    //let subTitle: String
}

var locs = [
    loc(title: "New York, NY",    latitude: 40.713054, longitude: -74.007228, phone: 2334), //subTitle: "Sub title"),
    ]

func placeAnnotations() {
    let annotations = locs.map { location -> MKAnnotation in
        let annotation = MKPointAnnotation()
        annotation.title = location.title
        annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)

        //annotation.subtitle = location.subTitle
        return annotation
    }
    mapView.addAnnotations(annotations)
}

(我有一个将数据添加到数组位置的函数)

这就是我在尝试添加不起作用的标注时得到的结果。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }
    let identifier = "pin"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true
        annotationView?.rightCalloutAccessoryView = UIButton(type: .infoDark)
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

您使用:

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

那肯定永远不会被调用。在Swift3中,正确的签名是:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?