我试图在 MapView 上显示两个注释。
为什么 MapView 中不显示第二个注释?我试图将 placemarks[0] 更改为 placemarks[1] 但没有任何帮助。
我本可以使用 for 子句,但为了测试我重复了代码。
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)
}
}
})
}