Search code examples
iosswiftmapkitmapkitannotation

swift 4 Annotation from json not visible until user drags on map


I am having problems showing the annotations on the map. They only show when I move or drag the map.

I tried to follow tutorial on youtube and also one of the questions which is c sharp

Please can sone one help .. here is the code

Here I created a class for the annotation

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

    init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
        self.title = pinTitle
        self.subtitle = pinSubTitle
        self.coordinate = location
    }}

    class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

     var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

        self.locationManager.requestWhenInUseAuthorization()


        //json show the list of the parks


pipiCanList()

     //show location Barcelona
    let location = CLLocationCoordinate2D(latitude: 41.3851   , longitude:2.1734)
    let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))

    self.mapView.setRegion(region, animated: true)
    self.mapView.delegate = self

}

get the json information

 func pipiCanList(){

        let url = "http://bluewave.lk/Apps/pipiCan/Api/read.php";
        let urlObj = URL(string:url);

        URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
            do{
                let pipiCans = try JSONDecoder().decode([pipiCanDBList].self, from: data!)

                for pipiCan in pipiCans{

                    let Latitude = (pipiCan.ParkLatitude! as NSString).doubleValue
                    let Longitude = (pipiCan.ParkLongitude! as NSString).doubleValue
                    let  location = CLLocationCoordinate2D(latitude: Latitude, longitude: Longitude)

                    //add to
                    let pin = customPin(pinTitle: pipiCan.ParkName!, pinSubTitle: pipiCan.Area!, location:  location)
                    self.mapView.addAnnotation(pin)

                  }
            } catch{

                print ("Error - cannot get list")

            }
            }.resume()


    }
}    

Solution

  • You need

    DispatchQueue.main.async { 
        let pin = customPin(pinTitle: pipiCan.ParkName!, pinSubTitle: pipiCan.Area!, location:  location)
        self.mapView.addAnnotation(pin)
    }
    

    As URLSession.shared.dataTask callback is in a background thread and you should access UIKit elements like mapView in the main thread You can also do

    self.mapView.showAnnotations(self.mapView.annotations, animated: true)
    

    after the for loop to show all added annotations also keep in mind you set a region here

     let location = CLLocationCoordinate2D(latitude: 41.3851   , longitude:2.1734)
    let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
    

    that the response may be with far location values than the above region