I'am trying to show two annotations at MapView.
Why second annotation doesn't show in MapView? I have tried to change placemarks[0] to place marks[1] but with no help.
I could have used for clause but for testing i repeated the code.
class Asiakas {
var nimi = ""
var osoite = ""
init(nimi: String, osoite: String) {
self.nimi = nimi
self.osoite = osoite
}
}
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
var asiakkaat:[Asiakas] = [
Asiakas(nimi: "Testi Asiakas", osoite: "Museokatu 10, helsinki"),
Asiakas(nimi: "Hyvä asiakas", osoite: "Pihlajatie 17, helsinki")
]
var asiakas:Asiakas!
override func viewDidLoad() {
super.viewDidLoad()
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(asiakkaat[0].osoite, completionHandler: { placemarks, error in
if error != nil {
print(error)
return
}
if let placemarks = placemarks {
let placemark = placemarks[0]
let annotation = MKPointAnnotation()
if let location = placemark.location {
annotation.coordinate = location.coordinate
self.mapView.addAnnotation(annotation)
}
}
})
geoCoder.geocodeAddressString(asiakkaat[1].osoite, completionHandler: { placemarks, error in
if error != nil {
print(error)
return
}
if let placemarks = placemarks {
let placemark = placemarks[0]
let annotation = MKPointAnnotation()
if let location = placemark.location {
annotation.coordinate = location.coordinate
self.mapView.addAnnotation(annotation)
}
}
})
}