Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKPolygon with holes

I searched over the internet but I couldn't find an answer to this. It is possible to draw a hole in a MKPolygon? Something like this:

enter image description here

I remember I saw something like this, but I'm not sure if it was related to iOS. It is possible to do this and (if it is) how I should start?

Thank you

like image 214
ov1d1u Avatar asked May 21 '13 14:05

ov1d1u


2 Answers

To do this properly, you should really look at the interiorPolygons parts of MKPolygon.

like image 198
incanus Avatar answered Oct 05 '22 22:10

incanus


As @incanus points out, you can define an interiorPolygons array. For example:

NSUInteger interiorCount = 5;
CLLocationCoordinate2D interiorCoordinates[interiorCount];

interiorCoordinates[0] = CLLocationCoordinate2DMake(...);
interiorCoordinates[1] = CLLocationCoordinate2DMake(...);
interiorCoordinates[2] = CLLocationCoordinate2DMake(...);
interiorCoordinates[3] = CLLocationCoordinate2DMake(...);
interiorCoordinates[4] = CLLocationCoordinate2DMake(...);

MKPolygon* interiorPolygon = [MKPolygon polygonWithCoordinates:interiorCoordinates
                                                         count:interiorCount];
interiorPolygon.title = @"interior polygon";

NSUInteger count = 5;
CLLocationCoordinate2D  coordinates[count];

coordinates[0] = CLLocationCoordinate2DMake(...);
coordinates[1] = CLLocationCoordinate2DMake(...);
coordinates[2] = CLLocationCoordinate2DMake(...);
coordinates[3] = CLLocationCoordinate2DMake(...);
coordinates[4] = CLLocationCoordinate2DMake(...);

MKPolygon* polygon = [MKPolygon polygonWithCoordinates:coordinates
                                                 count:count
                                      interiorPolygons:@[interiorPolygon]];

polygon.title = @"exterior polygon";
[self.mapView addOverlay:polygon];

That yields:

overlay with interior polygon

Credit goes to @incanus!

like image 22
Rob Avatar answered Oct 05 '22 23:10

Rob