Ron
Ron

Reputation: 1660

MKPolygon using Swift (Missing argument for parameter 'interiorPolygons' in call)

Fellow Devs, I'm trying to implement a polygon overlay on a mapview as follows:

private func drawOverlayForObject(object: MyStruct) {
    if let coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates {
        let polygon = MKPolygon(coordinates: coordinates, count: coordinates.count)
        self.mapView.addOverlay(polygon)
    }
}

The following error is presented:

Missing argument for parameter 'interiorPolygons' in call

According to the documentation: Apple Docu:

Mutable Pointers

When a function is declared as taking an UnsafeMutablePointer argument, it can accept any of the following:

  • nil, which is passed as a null pointer
  • An UnsafeMutablePointer value
  • An in-out expression whose operand is a stored lvalue of type Type, which is passed as the address of the lvalue
  • An in-out [Type] value, which is passed as a pointer to the start of the array, and lifetime-extended for the duration of the call

Now I think that my approach then would be correct, providing a [CLLocationCoordinate2D] array. Did anyone experience the same problem and found a workaround?

thanks Ronny

Upvotes: 4

Views: 2077

Answers (2)

Fabrizio Bartolomucci
Fabrizio Bartolomucci

Reputation: 4958

My final solution by cherry-picking from several tutorials and integrating:

func setPolylineFromPoints(locations:[CLLocation]){
    if locations.count == 0 {
        return;
    }
// while we create the route points, we will also be calculating the bounding box of our route
// so we can easily zoom in on it.
    var pt : UnsafeMutablePointer<MKMapPoint>? // Optional
    pt = UnsafeMutablePointer.alloc(locations.count)
    for idx in 0..<locations.count-1 {
       let location = locations[idx]
       let point = MKMapPointForCoordinate(location.coordinate);
       pt![idx] = point;
    }
    self.polyline = MKPolyline(points:pt!, count:locations.count-1)
// clear the memory allocated earlier for the points
    pt?.destroy()
    pt?.dealloc(locations.count)
}  

Upvotes: 0

Mike S
Mike S

Reputation: 42345

The error you're getting is Swift's cryptic way of saying that it can't find a method which matches your parameters. If you did try passing the interiorPolygons parameter, you'd get an equally confusing:

Extra argument 'interiorPolygons' in call

Your code is pretty close though; you just need a couple of minor changes. In the doc you reference, it says one of the things you can pass is:

An in-out [Type] value, which is passed as a pointer to the start of the array, and lifetime-extended for the duration of the call

So, it's looking for an in-out parameter. Which is done by passing coordinates prefixed with an &, like so:

MKPolygon(coordinates: &coordinates, count: coordinates.count)

But, in-out parameters can't be constants. From the docs:

You can only pass a variable as the argument for an in-out parameter. You cannot pass a constant or a literal value as the argument, because constants and literals cannot be modified.

So, you need to define coordinates with a var first:

if var coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates

Which makes the entire function look like this:

private func drawOverlayForObject(object: MyStruct) {
    if var coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates {
        let polygon = MKPolygon(coordinates: &coordinates, count: coordinates.count)
        self.mapView.addOverlay(polygon)
    }
}

Upvotes: 9

Related Questions