存在的问题:
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:)
初始化器作为第一个参数。