MKPolygon 初始化错误“调用中参数‘interiorPolygons’缺少参数”/“调用中存在额外参数”

2024-02-02

我正在尝试转换 MapKit 中的 Objective-C 代码MKPolygon参考文献清单6-9 https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6-SW1进入斯威夫特。

当我使用调用该函数时

 init(coordinates:count:)

init 函数,我收到错误:

调用中参数“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)

UPDATE:

 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)


(The 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
}


无关:而不是调用数组points, coordinates可能会更好,因为points意味着数组可能包含MKMapPoint结构体是什么(points:count:)初始化器作为第一个参数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MKPolygon 初始化错误“调用中参数‘interiorPolygons’缺少参数”/“调用中存在额外参数” 的相关文章

随机推荐