Confirm latlong is inside MKPolygon in iOS

I am checking if the latlong ( CLLocationCoordinate2D

) is inside the MKPolygon that is drawn on the MKMapview.

I am using below code to draw MKPolygon on MKMapview,

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;
}

      

I am checking latlong inside MKPolygon using

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);
    }
}

      

It works fine for normal cases, but if the user draws a polygon as shown below, then the MKpolygon has some intersecting points and the color is filled in some area and a clear color in some area.

enter image description here

If I pass a crisp colored part inside the MKPolygon to the Latlong, it should return NO. But it returns YES. those. if(CGPathContainsPoint(mpr , NULL, mapPointAsCGP, FALSE))

is TRUE.

How can I fix this problem while MKPolygon has an intersection between them? It is better if someone offers to fill the color in this transparent color area. Any suggestions would be greatly appreciated.

+3


source to share


2 answers


The image shown demonstrates the even-numbered rule. By specifying FALSE

on as the final parameter CGPathContainsPoint

, you asked him to apply the rule of the number of windings. Try to get through TRUE

.



For information on teo rules, see the Apple Quartz documentation , specifically "Filling the Path" (slightly less than half down).

+1


source


Swift 3.0 Implementation:



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)
}

      

0


source







All Articles