Добавьте заголовок и подзаголовок к связанным аннотациям в swift 4

Я показываю массив аннотаций в виде карты и должен иметь соответствующий заголовок и подзаголовок с каждой аннотацией. Мой текущий код только дает мне первый заголовок / подзаголовок во всех аннотациях.

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 ответ

Не большой сюрприз. Вы делаете цикл аннотаций, и вы говорите

    annotation.title = receivedAgencyEventSubTypeCode
    annotation.subtitle = receivedAgencyId

за каждую аннотацию через цикл. Аннотация отличается каждый раз. Но значения справа никогда не меняются, поэтому все заголовки и субтитры одинаковы.

Другие вопросы по тегам