为什么第二个 MKPointAnnotation 不显示?

Why second MKPointAnnotation doesn't show?

我正在尝试在 MapView 上显示两个注释。

为什么第二个注释没有显示在 MapView 中?我试图将 placemarks[0] 更改为 place marks[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)
                }
            }
        })

    }

您不能同时调用多个地理编码服务。

来自 geocodeAddressString(_ addressString: String, completionHandler: CoreLocation.CLGeocodeCompletionHandler) 的文档:

Submits a forward-geocoding request using the specified string. This method submits the specified location data to the geocoding server asynchronously and returns. Your completion handler block will be executed on the main thread. After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request.

因此您需要在第一个调用完成后进行第二个调用。

如果你这样做,你会得到正确的结果:

let geoCoder = CLGeocoder()

geoCoder.geocodeAddressString(asiakkaat[0].osoite, completionHandler: { placemarks, error in
    if error != nil {
        print(error)
        return
    }

    geoCoder.geocodeAddressString(self.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)
            }
        }
    })

    if let placemarks = placemarks {
        let placemark = placemarks[0]

        let annotation = MKPointAnnotation()
        if let location = placemark.location {
            annotation.coordinate = location.coordinate
            self.mapView.addAnnotation(annotation)
        }
    }
})