Search code examples
iosswiftmapkitios8mkpolygon

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.

When I call the function using the

 init(coordinates:count:)

init function, I get the error:

Missing argument for parameter 'interiorPolygons' in call

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)

UPDATE:

 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.


Solution

  • The problems with:

    var poly: MKPolygon = MKPolygon(points, 4)
    

    are that it doesn't give the argument labels for the initializer and it's not passing points as a pointer.

    Change the line to:

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


    (The points.withUnsafePointerToElements... version in your update will also work.)


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


    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
    }
    


    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.