我正在努力找出 MKPolygon 是否与 MKCircle 相交。
这是我的情况:我有一张充满地区的地图。现在用户可以在地图上设置一个大头针,我从那里绘制一个以大头针位置为中心的 MKCircle。现在我想知道这个圆圈是否与地图上已经存在的某个区域重叠。
我的想法是使用 CGPathContainsPoint 方法检查多边形的每个点是否位于圆内。
这是我的代码:
//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");
}
}
不幸的是,我得到了不可预测的结果——这没有任何意义。知道我做错了什么吗?还有另一种方法可以做我想做的事吗?
我的代码部分来自如何确定注释是否在 MKPolygonView (iOS) 中
注意:我知道我的解决方案依赖于以下假设:区域/路径定义了足够多的点,以便至少有一个点落在圆圈中。
提前致谢,
干杯,帕维