繁体   English   中英

viewForAnnotation没有被调用(使用Alamofire解析JSON)

[英]viewForAnnotation not being called (using Alamofire to parse JSON)

我是iOS编程的新手。 我更喜欢使用Swift。

我想做的是调用一个返回一些JSON的Web服务,将JSON解析为一个名为Entry的自定义对象,然后在地图上绘制这些条目。

我正在使用AlamoFire进行Web服务调用,并使用SwiftyJSON将JSON解析为Entry对象,如下所示

    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)

                }
            }
    }

因此,如您在上面的代码中看到的那样,我还映射了AlamoFire请求中的条目(不知道这是否不好,因为AlamoFire是在我相信的单独线程上完成的)。

我也有一个viewForAnnotations方法,如下所示

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

         return nil
    }

我的viewForAnnotation方法根本没有被调用(未击中打印行的断点)。

任何帮助表示赞赏!

请确保将mapview的委托属性设置为self。

mapview.delegate = self

您也可以通过使用Connection Inspector(Interface Builder)将mapview的代理出口连接到ViewController来完成此操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM