Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Alphonso Sensley II
Alphonso Sensley II
8,514 Points

What is the best way to create map annotations from an Array of coordinates?

Sorry in advance for the long question; but I think background of my code is helpful for anyone trying to answer.

I am having trouble displaying multiple locations onto a mapView. I retrieve the latitude and longitude values from an API as an Array. I am having trouble displaying all of the instances onto the mapView rather than just a single location from the array.

Here is my model, the location1 property is the important one for this question.

struct CrimeReport: Codable {
    var block: String
    var crimeDescription: String
    var city: String
    var location1 : Location1
    var date: String
    var state: String

    enum CodingKeys: String, CodingKey {
        case block = "block"
        case crimeDescription = "crimedescription"
        case city = "city"
        case date = "datetime"
        case state = "state"
        case location1 = "location_1"
    }
}
struct Location1: Codable {
    let type: String
    let coordinates: [Double]
}

For my data coming from the API; I created CrimeData as an array of my model type eg: CrimeData = [CrimeReport] I then use the .map method to create an Array of array coordinates...

var coordinatesArray = CrimeData.map {$0.location1.coordinates}

So, coordinatesArray = [[Double]], and the data in coordinatesArray looks like this:

[[-122.08945056, 37.674395599999997], [-122.08945056, 37.674395599999997], [-122.08945056, 37.674395599999997], [-122.08945056, 37.674395599999997]]

So my question is; How do I display all of the locations in coordinatesArray onto my mapView? I figured out how to display one location on the map, but can't quite get how to display the locations within my coordinatesArray variable.

Here is my incomplete code for my mapView so far, not sure what to assign to latitude and longitude :

 let annotations = [MKAnnotation]()

            let latitide =
            let longitude =

            //Create Annotations

            let annotation = MKPointAnnotation()
            annotation.title = CrimeData[0].crimeDescription
            annotation.subtitle = CrimeData[0].date
            annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude)
            self.mapView.addAnnotation(annotation)


            //Zooming In On Annotation
            let coordinates: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
            let span = MKCoordinateSpanMake(0.1, 0.1)
            let region = MKCoordinateRegionMake(coordinates, span)
            self.mapView.setRegion(region, animated: true)

Hopefully my description and question was not too confusing. Any help from this great TreeHouse community will be greatly appreciated!