2

我正在努力找出 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) 中

注意:我知道我的解决方案依赖于以下假设:区域/路径定义了足够多的点,以便至少有一个点落在圆圈中。

提前致谢,

干杯,帕维

4

2 回答 2

3

嗯,我终于想通了。上面的代码应该按原样工作,除了循环中缺少 break 语句。原样的代码仅返回多边形的最后一个检查点。向 mapCoordinateIsInPolygon 插入一个测试,然后在内部循环中的 break 语句在第一个测试为正时立即离开循环,从而给出正确的结果。;-)

于 2011-08-05T06:50:50.027 回答
0
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);
}
于 2015-02-12T05:01:57.007 回答