快速为关联的注释添加标题和副标题

时间:2018-12-16 02:58:29

标签: ios swift mapkit mapkitannotation

我正在地图视图中显示注释数组,并且每个注释都需要具有相应的标题和副标题。我当前的代码仅使我获得所有注释的第一个标题/副标题。

func multiPoint() {
    var coordinateArray: [CLLocationCoordinate2D] = []
    if receivedArrayOfLats.count == receivedArrayOfLongs.count {
        for i in 0 ..< receivedArrayOfLats.count {
            let eventLocation = CLLocationCoordinate2DMake(receivedArrayOfLats[i], receivedArrayOfLongs[i])
            coordinateArray.append(eventLocation)
        }
    }

    for events in coordinateArray {
        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: events.latitude, longitude: events.longitude)
        annotation.title = receivedAgencyEventSubTypeCode
        annotation.subtitle = receivedAgencyId
        multiEventMap?.addAnnotation(annotation)
        }
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "annotationView"
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
    if #available(iOS 11.0, *) {
        if view == nil {
            view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        }
        view?.displayPriority = .required
    } else {
        if view == nil {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        }
    }
    view?.annotation = annotation
    view?.canShowCallout = true
    return view
}

1 个答案:

答案 0 :(得分:1)

毫不奇怪。您循环制作注释,您在说

    annotation.title = receivedAgencyEventSubTypeCode
    annotation.subtitle = receivedAgencyId

对于循环中的每个注释。注释每次都不同。但是右侧的值永远不会改变,因此所有标题和字幕都相同。