【问题标题】:Validate if a latlong is inside a MKPolygon in iOS验证 latlong 是否在 iOS 的 MKPolygon 内
【发布时间】:2014-11-13 04:01:24
【问题描述】:

我正在验证 latlong(CLLocationCoordinate2D) 是否在绘制在 MKMapview 上的 MKPolygon 内。

我正在使用下面的代码在 MKMapview 上绘制 MKPolygon,

MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coordinates count:count];
[mapviewcontroller.mapview addOverlay:polygon];

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
    renderer.fillColor   = [[UIColor grayColor] colorWithAlphaComponent:0.2];
    renderer.strokeColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    renderer.lineWidth   = 2;
    return renderer;
}

我正在使用 MKPolygon 验证 latlong,

CLLocationCoordinate2D sampleLocation = CLLocationCoordinate2DMake(13,80);//13,80 is the latlong of clear colored area of the MKPolygon in the below image.
MKMapPoint mapPoint = MKMapPointForCoordinate(sampleLocation);
CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);

for (id<MKOverlay> overlay in mapview.overlays) {
    if([overlay isKindOfClass:[MKPolygon class]]){
        MKPolygon *polygon = (MKPolygon*) overlay;

        CGMutablePathRef mpr = CGPathCreateMutable();

        MKMapPoint *polygonPoints = polygon.points;

        for (int p=0; p < polygon.pointCount; p++){
            MKMapPoint mp = polygonPoints[p];
            if (p == 0)
                CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
            else
                CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
        }

        if(CGPathContainsPoint(mpr , NULL, mapPointAsCGP, FALSE)){
            isInside = YES;
        }

        CGPathRelease(mpr);
    }
}

它在正常情况下工作得很好,但是如果用户像下面这样绘制多边形,即 MKpolygon 有一些相交的点,并且在某些区域填充颜色并且在某些区域清除颜色。

如果我通过 MKPolygon 内透明颜色部分的纬度,它应该返回 NO。但是,它返回的是。即,if(CGPathContainsPoint(mpr , NULL, mapPointAsCGP, FALSE)) 是 TRUE。

当 MKPolygon 之间有交集时,我该如何解决这个问题?如果有人建议在那个清晰的颜色区域填充颜色,那就更好了。任何建议将不胜感激。

【问题讨论】:

    标签: ios objective-c mkmapview mapkit mkpolygon


    【解决方案1】:

    显示的图片似乎展示了奇偶填充规则。通过将FALSE 指定为CGPathContainsPoint 的最终参数,您已经要求它应用缠绕数规则。尝试传递TRUE

    有关 teo 规则的信息,请参阅 Apple's Quartz documentation,尤其是“Filling a Path”(略低于一半)。

    【讨论】:

    • 工作出色!。非常感谢汤米。
    【解决方案2】:

    Swift 3.0 实现:

    func checkIf(polygon: MKPolygon, contains point: CGPoint) -> Bool {
        let path = CGMutablePath()
        let polygonPoints = polygon.points()
    
        for index in 0..<polygon.pointCount {
            let mp = polygonPoints[index]
    
            if index == 0 {
                path.move(to: CGPoint(x: mp.x, y: mp.y))
            } else {
                path.addLine(to: CGPoint(x: mp.x, y: mp.y))
            }
        }
    
        return path.contains(point)
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2011-04-24
      相关资源
      最近更新 更多