【问题标题】:MKPolygon initialization error "Missing argument for parameter 'interiorPolygons' in call" / "Extra argument in call"MKPolygon 初始化错误“调用中的参数“interiorPolygons”缺少参数”/“调用中的额外参数”
【发布时间】:2014-08-04 20:46:03
【问题描述】:

我正在尝试将 Listing 6-9 中的 MapKit MKPolygon 引用中的 Objective-C 代码转换为 Swift。

当我调用函数时使用

 init(coordinates:count:)

初始化函数,我得到错误:

调用中的参数“interiorPolygons”缺少参数

当我使用interiorPolygons 参数调用函数时,我得到了错误:

调用中的额外参数

这是我正在使用的代码。

 var points: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()

 points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116)
 points[1] = CLLocationCoordinate2DMake(41.002371, -102.052066)
 points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981)
 points[3] = CLLocationCoordinate2DMake(36.99892, -109.045267)

 var poly: MKPolygon = MKPolygon(points, 4)

 poly.title = "Colorado"
 theMapView.addOverlay(poly)

更新:

 points.withUnsafePointerToElements() { (cArray: UnsafePointer<CLLocationCoordinate2D>) -> () in
            poly = MKPolygon(coordinates: cArray, count: 4)
        }

似乎摆脱了编译器错误,但仍然没有添加覆盖。

【问题讨论】:

    标签: ios swift mapkit ios8 mkpolygon


    【解决方案1】:

    问题:

    var poly: MKPolygon = MKPolygon(points, 4)
    

    是它没有为初始化程序提供参数标签,也没有将points 作为指针传递。

    将行改为:

    var poly: MKPolygon = MKPolygon(coordinates: &points, count: 4)
    


    (您更新中的points.withUnsafePointerToElements... 版本也可以使用。)


    另请注意,var points: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]() 创建了一个 empty 数组。执行points[0] = ... 应该会导致运行时错误,因为数组没有以开头的元素。相反,使用points.append() 将坐标添加到数组中:

    points.append(CLLocationCoordinate2DMake(41.000512, -109.050116))
    points.append(CLLocationCoordinate2DMake(41.002371, -102.052066))
    points.append(CLLocationCoordinate2DMake(36.993076, -102.041981))
    points.append(CLLocationCoordinate2DMake(36.99892, -109.045267))
    

    或者只是一起声明和初始化:

    var points = [CLLocationCoordinate2DMake(41.000512, -109.050116),
                  CLLocationCoordinate2DMake(41.002371, -102.052066),
                  CLLocationCoordinate2DMake(36.993076, -102.041981),
                  CLLocationCoordinate2DMake(36.99892, -109.045267)]
    


    如果您仍然看不到覆盖,请确保您已实现 rendererForOverlay 委托方法(并设置或连接地图视图的 delegate 属性):

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolygon {
            var polygonRenderer = MKPolygonRenderer(overlay: overlay)
            polygonRenderer.fillColor = UIColor.cyanColor().colorWithAlphaComponent(0.2)
            polygonRenderer.strokeColor = UIColor.blueColor().colorWithAlphaComponent(0.7)
            polygonRenderer.lineWidth = 3
            return polygonRenderer
        }
    
        return nil
    }
    


    不相关:比起调用数组pointscoordinates 可能会更好,因为points 暗示数组可能包含MKMapPoint 结构,这是(points:count:) 初始化程序作为第一个参数的结构。

    【讨论】:

    • 谢谢!我的回答在技术上解决了我的问题,但你的回答更彻底,代码也更清晰。
    猜你喜欢
    • 1970-01-01
    • 2019-11-25
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多