I'm struggling with finding out if a MKPolygon intersects with a MKCircle.
This is my situation: I have a map filled with regions. Now the user can set a pin on the map, from where I draw an MKCircle with the pin's location as the center. Now I want to know if this circle overlaps with some region already on the map.
My idea is to check every point of the polygons if they lie within the circle using the CGPathContainsPoint method.
This is my code:
//the circle for the location filter
MKCircleView *circleView = (MKCircleView *)[map viewForOverlay:self.sfvc.pinCircle];
//loop all regions
for (MKPolygon *region in regions){
BOOL mapCoordinateIsInPolygon = FALSE;
//loop all point of this region
for (int i = 0; i < region.pointCount; i++) {
MKMapPoint point = region.points[i];
CGPoint circleViewPoint = [circleView pointForMapPoint:point];
mapCoordinateIsInPolygon = CGPathContainsPoint(circleView.path, NULL, circleViewPoint, NO);
}
if (mapCoordinateIsInPolygon) {
NSLog(@"YES! At least one point of the poly lies within the circle");
}
}
Unfortunately I get unpredictable results - that do not make any sense. Any idea what I'm doing wrong? Is there another way to do what I want?
My code is partly from How to determine if an annotation is inside of MKPolygonView (iOS)
Note: I know that my solution relies on the assumption that the regions / paths have enough points defined so that at least one point will fall in the circle.
Thanks in advance,
Cheers, pawi