注释标注不工作Swift

时间:2017-08-30 11:27:07

标签: ios swift swift3 mapkit mkannotationview

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

1 个答案:

答案 0 :(得分:2)

您使用:

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

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

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