5

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

ov1d1u
  • 958
  • 1
  • 17
  • 38

2 Answers2

8

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

incanus
  • 5,100
  • 1
  • 13
  • 20
2

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!

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Anyway to add a rounded whole? – Andrespch May 13 '15 at 15:13
  • Sure, you could replace that `interiorPolygon` with a many sided equilateral polygon. If there are enough sides to it (e.g. 60? 90? 120? etc.), I suspect it will be visually indistinguishable from a circle. But, no, I don't see anyway to use a `MKCircle` in lieu of the `interiorPolygons`. – Rob May 13 '15 at 15:22
  • can you make the inside polygons to have a color? basinal the inverse of what you did? – Igal Flegmann Jun 21 '16 at 16:30
  • @pudm - just add another polygon for the center. – Rob Jun 21 '16 at 16:52
  • @Rob what i meant is to have polygons only inside a main polygon so basically like holes except polygons and remove everything that is outside – Igal Flegmann Jun 21 '16 at 17:01
  • I don't understand what you mean. You should create a graphical representation of what you mean and then post your own question rather than asking in comments here. – Rob Jun 21 '16 at 17:14