I am working in an IOS 7 project ,it contains a location checking (current location is in given polygons).
I am Using the following code to check the condition
Created an array of MKPolygons
for(MKPolygon *poly in self.polygonArray)
{
[self checkTheLocationIsInPolygon:currentLocation polygon:poly];
}
- (void)checkTheLocationIsInPolygon:(CLLocation*)aLocation polygon:(MKPolygon*)aPolygon
{
CLLocationCoordinate2D coordinate = {aLocation.coordinate.latitude, aLocation.coordinate.longitude};
MKMapPoint mapPoint = MKMapPointForCoordinate(coordinate);
CGMutablePathRef mpr = CGPathCreateMutable();
MKMapPoint *polygonPoints = aPolygon.points;
size_t nCount = aPolygon.pointCount;
for (int p = 0; p < nCount; 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);
CGPathRelease(mpr);
if(pointIsInPolygon == YES)
{
//IN
}
else
{
//Out
}
}
This code is working correctly(pointIsInPolygon return YES/NO correctly) for the first polygon .Then the next iteration (Next polygon from array) pointIsInPolygon gives the previous state means, it return NO if the first polygon was outside the location and it return YES if the first polygon was inside the location .
How to fix this issue?
If anybody know, please give me a suggestion