ios - MKPolygon 初始化错误 "Missing argument for parameter ' interiorPolygons' in call"/"Extra argument in call"

标签 ios swift mapkit ios8 mkpolygon

我正在尝试转换 MapKit MKPolygon 中的 Objective-C 代码,引用 Listing 6-9进入 swift 。

当我调用函数时

 init(coordinates:count:)

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

Missing argument for parameter 'interiorPolygons' in call

当我使用 interiorPolygons 参数调用该函数时,出现错误:

Extra argument in call

这是我正在使用的代码。

 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)
        }

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

最佳答案

存在的问题:

var poly: MKPolygon = MKPolygon(points, 4)

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

将行更改为:

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


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


另请注意,var points: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]() 创建了一个 数组。执行 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)方法(并设置或连接 map View 的 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:) 初始值设定项的第一个参数。

关于ios - MKPolygon 初始化错误 "Missing argument for parameter ' interiorPolygons' in call"/"Extra argument in call",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25127239/

相关文章:

ios - 为什么 CoreLocation 无法获取新的 GPS 数据而 Mapkit 显示旧的 GPS 数据

ios - 理解 og :url

ios - 从 xcassets 中的图像创建 URL 总是返回 nil

ios - 检查 Firestore 查询 (whereField isEqualTo) 是否未找到任何文档

objective-c - 延迟加载 UIView 直到 Mapkit 加载

xcode - 无法加载 MapKit 模块

ios - 从 objective-c 中的主线程返回值

android - 如何在Android中使用Phonegap实现 "one time login"?

ios - 构建应用程序时出现编译器/链接器错误

ios - 为什么我的 NSNotificationCenter 抛出异常?