Search code examples
arraysswiftannotationsmapkit

Calculating total latitude/longitude of annotations in Swift 4 mapKit?


So I have a navigation application using the mapKit, and I have a button that allows the user to add multiple annotations if they would like. I should add that they add these by searching for a location in a searchtable, and pressing it, creating the annotation, like normal maps apps. My button allows them to add multiple.

1) How can I add the latitudes/longitudes of each annotation? I was thinking something along the lines of a

let totalLatitude = mapView.annotations.reduce etc etc

Will this work?

2) What parameters should I use if it is a reduce?

3) How can I put it in a form that allows me to specify like mapView.annotations.coordinate.latitude etc etc?

4) I know you can't put .coordinate with .annotations... so is there another way? thanks


Solution

  • If you mean you want the sum of the latitudes/longitudes of all annotations:

    let totalLatitudes = mapView.annotations.reduce(0) { $0 + $1.coordinate.latitude }
    
    let totalLongitudes = mapView.annotations.reduce(0) { $0 + $1.coordinate.longitude }