0

我是 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 方法根本没有被调用(打印行上的断点没有被击中)。

任何帮助表示赞赏!

4

1 回答 1

2

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

mapview.delegate = self

您也可以通过使用 Connection Inspector (Interface Builder) 将 mapview 的代理出口连接到 ViewController 来做到这一点

于 2015-07-22T05:33:40.400 回答