1

I would like to create a MKPolygon to display on a MKMapView. My problem is that I'm not able to figure out how to do it.

I know that to create an MKPolygon I have to create a bunch of MKMapPoint structs and put them into an array and call the class method polygonWithPoints.

My problem is that I have an NSArray containing CLLocation objects that have coordinate.latitude and coordinate.longitude properties.

How do I convert it one by one to an MKMapPoint struct?

Mppl
  • 941
  • 10
  • 18

1 Answers1

3

If you have an NSArray of objects containing coordinates, it will be easier to use the polygonWithCoordinates:count: method instead of polygonWithPoints:count:.

The polygonWithCoordinates:count: method accepts a C array of CLLocationCoordinate2D structs. The coordinate property in a CLLocation object is also a CLLocationCoordinate2D.

If you still want to use polygonWithPoints:count:, you can use the MKMapPointForCoordinate function to convert the coordinate property in the CLLocation to an MKMapPoint.

With either method, you first create a C array of the appropriate struct, loop through the NSArray to set each item in the C array. Then call polygonWithCoordinates or polygonWithPoints.

This answer has a code example using polygonWithCoordinates. In that example, you would change the two lines in the for loop to:

CLLocation *coordObj = (CLLocation *)[coordinateData objectAtIndex:i];
coords[i] = coordObj.coordinate;

Don't forget to implement the viewForOverlay delegate method (and make sure the map view's delegate property is set).

Community
  • 1
  • 1