-2

I'm showing an array of annotations in a map view and need to have the corresponding title and subtitle with each annotation. My current code only gets me the 1st title/subtitle on all of the annotations.

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
}
jvan
  • 173
  • 1
  • 9

1 Answers1

1

No big surprise. You loop making annotations, and you are saying

    annotation.title = receivedAgencyEventSubTypeCode
    annotation.subtitle = receivedAgencyId

for every annotation thru the loop. The annotation is different each time. But the values on the right side never change so all the titles and subtitles are the same.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I see what you’re saying but can I add more data to the CLLocationCoordinate2D array or just lat/long – jvan Dec 16 '18 at 21:01