注释呼叫无法正常工作



我正在尝试向我在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)
}

(我有一个函数,将数据添加到数组LOC)

这是我尝试添加不起作用的标注时所拥有的。

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!

肯定不会被称为。在Swift 3中,正确的签名是:

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

最新更新