how to set up array for multi annotations with swift
Asked Answered
M

1

12

How should the array below be set. Im trying to add multiple annotations onto my map. I was able to find the code below on stackoverflow but they did not show how to set up the array.

var objects = [ 
                //how should the array be setup here 
              ]

for objecters in objects!{
    if let latit = objecters["Coordinates"]["Latitude"]{
        self.latitudepoint = latit as! String
        self.map.reloadInputViews()
    }
    else {
        continue
    }
    if let longi = objecters["Coordinates"]["Longitude"]{
        self.longitudepoint = longi as! String
        self.map.reloadInputViews()
    }
    else {
        continue
    }
    var annotation = MKPointAnnotation()
    var coord = CLLocationCoordinate2D(latitude: Double(self.latitudepoint)!,longitude: Double(self.longitudepoint)!)
    mapView.addAnnotation(annotation)
}
Muticous answered 16/4, 2016 at 16:52 Comment(1)
Thank you for the reply rob. Each array should contain 3 items latitude, longitude and string. i want to use the coordinates to plot the annotations and when the annotation is tapped by the user the the string will appear and if the string is tapped it will open a different view controller giving access to to reuse the stringMuticous
G
36

You could do, for example:

let locations = [
    ["title": "New York, NY",    "latitude": 40.713054, "longitude": -74.007228],
    ["title": "Los Angeles, CA", "latitude": 34.052238, "longitude": -118.243344],
    ["title": "Chicago, IL",     "latitude": 41.883229, "longitude": -87.632398]
]

for location in locations {
    let annotation = MKPointAnnotation()
    annotation.title = location["title"] as? String
    annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double)
    mapView.addAnnotation(annotation)
}

Or, alternatively, use a custom type, e.g.:

struct Location {
    let title: String
    let latitude: Double
    let longitude: Double
}

let locations = [
    Location(title: "New York, NY",    latitude: 40.713054, longitude: -74.007228),
    Location(title: "Los Angeles, CA", latitude: 34.052238, longitude: -118.243344),
    Location(title: "Chicago, IL",     latitude: 41.883229, longitude: -87.632398)
]

for location in locations {
    let annotation = MKPointAnnotation()
    annotation.title = location.title
    annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
    mapView.addAnnotation(annotation)
}

Or you can replace that for loop with map:

let annotations = locations.map { location -> MKAnnotation in
    let annotation = MKPointAnnotation()
    annotation.title = location.title
    annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
    return annotation
}
mapView.addAnnotations(annotations)

Finally, it is worth noting that you can make your custom object conform to MKAnnotation protocol, and then you can use it directly, bypassing MKPointAnnotation type entirely:

class Location: NSObject, MKAnnotation {
    dynamic var title: String?
    dynamic var subtitle: String?
    dynamic var coordinate: CLLocationCoordinate2D

    convenience init(title: String? = nil, subtitle: String? = nil, latitude: Double, longitude: Double) {
        self.init(title: title, subtitle: subtitle, coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
    }

    init(title: String? = nil, subtitle: String? = nil, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate

        super.init()
    }
}

let locations = [
    Location(title: "New York, NY",    latitude: 40.713054, longitude: -74.007228),
    Location(title: "Los Angeles, CA", latitude: 34.052238, longitude: -118.243344),
    Location(title: "Chicago, IL",     latitude: 41.883229, longitude: -87.632398)
]

mapView.addAnnotations(locations)

Note, the use of the dynamic qualifier is optional, but is useful if you intend to make your annotation views draggable and/or have annotation mutations be reflected dynamically on the map.

Graduation answered 16/4, 2016 at 18:25 Comment(2)
perfect that is exactly what i was looking for. Thank you for the great helpMuticous
i never did realize i had to accepted but i just did to your answer i will be going back to all my other questions and selecting the answer that worked for meMuticous

© 2022 - 2024 — McMap. All rights reserved.