0

I used coordinate from API to put an annotation on map, but it didn't show up while:

  1. I declared a new coordinate which is not from API, can show up on map.
  2. I can print the coordinate form API, and the coordinate was fine.
  3. I put mapView.addAnnotation() in DispatchQueue.main.async.

Can anyone tell me how to fix it? Thanks! Here is my code:

extension LocationViewController: ParkProviderDelegate {
func didFetch(by provider: ParkProvider) {

    DispatchQueue.main.async {
        let annotation = MKPointAnnotation()
        let coordinate = self.provider.parks[0].coordinate
        self.mapView?.addAnnotation(annotation)
    }
}
Ciao
  • 56
  • 5

1 Answers1

0

Try the following code:

DispatchQueue.main.async {
    let annotation = MKPointAnnotation()
    let coordinate = self.provider.parks[0].coordinate
    annotation.coordinate = coordinate
    annotation.title = "title"
    annotation.subtitle = "subtitle"
    self.mapView?.addAnnotation(annotation)
}
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52