检查地图上的点是否位于叠加层内
我正在努力找出 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) 内部
注意:我知道我的解决方案依赖于这样的假设:区域/路径定义了足够的点,以便至少有一个点会落入圆圈内。
预先感谢,
干杯,pawi
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我终于想通了。上面的代码应该按原样工作,只是循环中缺少一个break语句。代码按原样仅返回多边形的最后一个检查点。将测试插入到mapCooperativeIsInPolygon,然后在第一个测试为正时,内部循环中的break语句就会离开循环,从而给出正确的结果。 ;-)
Well, I figured it out finally. The code above should work as is, except that there is a missing break statement in the loop. The code as is returns only the last checked point of the poly. Inserting a test to mapCoordinateIsInPolygon and then a break statement in the inner loop leaves the loop as soon as the first test is positive, thus giving the correct result. ;-)