Search code examples
swiftalamofireswifty-jsonmapkitannotation

viewForAnnotation not being called (using Alamofire to parse JSON)


I'm new to iOS programming. I prefer to use Swift.

What I'm trying to do is call a web service that returns some JSON, parse the JSON into a custom object called Entry and plot those entries on a map.

I am using AlamoFire to make the web service call and SwiftyJSON to parse the JSON into Entry objects like below

    request(.GET, URLString: "https://myURLThatReturnsJson")
        .responseJSON { (_, _, json, error) in

            if let json: AnyObject = json{

                let jsonEntries = JSON(json)

                for result in jsonEntries["entries"].arrayValue {
                    let business_name = result["business_name"].stringValue
                    let latitude = result["lat"].stringValue
                    let longitude = result["lng"].stringValue
                    let category = result["category"].stringValue
                    let address = result["address"].stringValue
                    let city = result["city"].stringValue
                    let type = result["type"].stringValue
                    let location = result["location"].stringValue
                    let valid = result["valid"].stringValue
                    let comments = result["comments"].stringValue
                    let date = result["date"].stringValue
                    let username = result["username"].stringValue
                    let additional_comment_count = result["additional_comment_count"].stringValue


                    let entry : Entry = Entry(
                        business_name: business_name,
                        latitude: latitude,
                        longitude: longitude,
                        category: category,
                        address: address,
                        city: city,
                        type: type,
                        location: location,
                        valid: valid,
                        comments: comments,
                        date: date,
                        username: username,
                        additional_comment_count: additional_comment_count,
                        title: business_name,
                        subtitle: location,
                        coordinate: CLLocationCoordinate2D(latitude: (latitude as NSString).doubleValue, longitude: (longitude as NSString).doubleValue))

                    entries.append(entry)
                }

                // Now we have our array of entries. Now plot them.

                for entry in entries{

                    self.mapView.addAnnotation(entry)

                }
            }
    }

So as you can see in the code above I am also mapping the entries within the AlamoFire request (don't know if this is bad because AlamoFire is done on a separate thread I believe).

I also have a viewForAnnotations method that looks like below

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
         print("viewForAnnotation called")

         return nil
    }

My viewForAnnotation method is not being called at all (break point on print line is not being hit).

Any help is appreciated!


Solution

  • Please make sure to set the delegate property of mapview to self.

    mapview.delegate = self
    

    You can also do this by connecting the delegate outlet of mapview to ViewController using Connection Inspector (Interface Builder)