MKPolygon初始化错误“在调用中参数'interiorPolygons'缺少参数” /“呼叫中的额外参数” [英] MKPolygon initialization error "Missing argument for parameter 'interiorPolygons' in call" / "Extra argument in call"
问题描述
I'm trying to convert the Objective-C code in the MapKit MKPolygon
reference in Listing 6-9 into Swift.
当我使用
init(coordinates:count:)
初始化函数,我收到错误:
init function, I get the error:
在调用中缺少参数'interiorPolygons'的参数
Missing argument for parameter 'interiorPolygons' in call
当我调用函数时interiorPolygons参数,我收到错误:
When I call the function with the interiorPolygons argument, I get the error:
调用中的额外参数
Extra argument in call
这是我正在使用的代码。
Here is the code that I am using.
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)
}
似乎摆脱了编译器错误,但仍然没有添加叠加层。
seems to get rid of the compiler error, but still doesn't add an overlay.
推荐答案
以下问题:
var poly: MKPolygon = MKPolygon(points, 4)
它不给初始化程序的参数标签,并且它没有将 points
作为指针传递。
are that it doesn't give the argument labels for the initializer and it's not passing points
as a pointer.
将行更改为:
var poly: MKPolygon = MKPolygon(coordinates: &points, count: 4)
(更新中的 points.withUnsafePointerToElements ...
版本也会有效。)
另请注意 var points:[CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
创建一个空数组。执行 points [0] = ...
应该会导致运行时错误,因为数组没有元素可以开始。而是使用 points.append()
将坐标添加到数组中:
Also note that var points: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
creates an empty array. Doing points[0] = ...
should cause a run-time error since the array has no elements to begin with. Instead, add the coordinates to the array using 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))
或者只是一起声明和初始化:
or just declare and initialize together:
var points = [CLLocationCoordinate2DMake(41.000512, -109.050116),
CLLocationCoordinate2DMake(41.002371, -102.052066),
CLLocationCoordinate2DMake(36.993076, -102.041981),
CLLocationCoordinate2DMake(36.99892, -109.045267)]
< br>
如果仍然没有看到叠加层,请确保已实现 rendererForOverlay
委托方法(并设置或连接地图视图的委托
property):
If you still don't see the overlay, make sure you've implemented the rendererForOverlay
delegate method (and set or connected the map view's delegate
property):
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:)
初始值设定项作为第一个参数。
Unrelated: Rather than calling the array points
, coordinates
might be better because points
implies the array might contain MKMapPoint
structs which is what the (points:count:)
initializer takes as the first argument.
这篇关于MKPolygon初始化错误“在调用中参数'interiorPolygons'缺少参数” /“呼叫中的额外参数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!