检查地图上的点是否位于叠加层内

发布于 2024-11-18 12:37:15 字数 1238 浏览 3 评论 0原文

我正在努力找出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2024-11-25 12:37:15

好吧,我终于想通了。上面的代码应该按原样工作,只是循环中缺少一个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. ;-)

一刻暧昧 2024-11-25 12:37:15
CLLocationCoordinate2D newCoord ;
            newCoord.latitude = [[firDict objectForKey:@"Latitude"] floatValue];
            newCoord.longitude = [[firDict objectForKey:@"Longitude"] floatValue];

            [self pointInsideOverlay:newCoord MyOverlay:Curoverlay];

-(void)pointInsideOverlay:(CLLocationCoordinate2D )tapPoint MyOverlay:(id <MKOverlay>)overlay
{
    isInside = FALSE;
    MKMapPoint mapPoint = MKMapPointForCoordinate(tapPoint);
    CGMutablePathRef mpr = CGPathCreateMutable();
    MKMapPoint *polygonPoints = myPolygon.points;
    for (int p=0; p < myPolygon.pointCount; p++)
    {
        MKMapPoint mp = polygonPoints[p];
        if (p == 0)
            CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
        else
            CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
    }

    CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);
    BOOL pointIsInPolygon = CGPathContainsPoint(mpr, NULL, mapPointAsCGP, FALSE);
    if (pointIsInPolygon)
    {
        isInside = TRUE;
    }

    CGPathRelease(mpr);
}
CLLocationCoordinate2D newCoord ;
            newCoord.latitude = [[firDict objectForKey:@"Latitude"] floatValue];
            newCoord.longitude = [[firDict objectForKey:@"Longitude"] floatValue];

            [self pointInsideOverlay:newCoord MyOverlay:Curoverlay];

-(void)pointInsideOverlay:(CLLocationCoordinate2D )tapPoint MyOverlay:(id <MKOverlay>)overlay
{
    isInside = FALSE;
    MKMapPoint mapPoint = MKMapPointForCoordinate(tapPoint);
    CGMutablePathRef mpr = CGPathCreateMutable();
    MKMapPoint *polygonPoints = myPolygon.points;
    for (int p=0; p < myPolygon.pointCount; p++)
    {
        MKMapPoint mp = polygonPoints[p];
        if (p == 0)
            CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
        else
            CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
    }

    CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);
    BOOL pointIsInPolygon = CGPathContainsPoint(mpr, NULL, mapPointAsCGP, FALSE);
    if (pointIsInPolygon)
    {
        isInside = TRUE;
    }

    CGPathRelease(mpr);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文