I'm busy making an application where I want an UIAlerView
to be shown, if an coordinate hits one of the Polygon.
I got the this, and it works fine:
for (id<MKOverlay> overlay in myMapView.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);
}
CGPoint mapPointAsCGP = CGPointMake(mapPointToTest.x, mapPointToTest.y);
BOOL pointIsInPolygon = CGPathContainsPoint(mpr, NULL, mapPointAsCGP, FALSE);
NSLog(@"Coordinate %f,%f is in polygon %@: %@",
coordinateToTest.latitude, coordinateToTest.longitude,
polygon.title,
(pointIsInPolygon ? @"Yes" : @"No"));
if (pointIsInPolygon == 1) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Uw vuilnisdag" message:@"Op dinsdag 06:00 en vrijdag 06:00" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Stel notificatie in", nil];
[alertView show];
} else {
UIAlertView *alertViewN = [[UIAlertView alloc]initWithTitle:@"Sorry" message:@"Geen ophaaldag gevonden in uw gebied" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[alertViewN show];
}
CGPathRelease(mpr);
}
}
In the for loop the CGPathContainsPoint
code checks all MKPolygon
.
In the end I get a result: 1 or 0.
- 1 = means there is an point in one of the Polygons
- 0 = means there is no point found in one the Polygons
The problem now is that, if the result is 0, I get like 8 times an AlertView.
Is there an easy way to get rid of this?
Thanks
Update 1
This is the alert View delegate.
Not sure what value to put in buttonIndex == 0.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0 ){
//
}
}
Update 2
This is what I've got so far.
I was playing around with the isKindOfClass:[NSString class]
.. Didn't worked out.
BOOL pointIsInPolygon = CGPathContainsPoint(mpr, NULL, mapPointAsCGP, FALSE);
NSLog(@"Coordinate %f,%f is in polygon %@: %@",
coordinateToTest.latitude, coordinateToTest.longitude,
polygon.title,
(pointIsInPolygon ? @"Yes" : @"No"));
if (pointIsInPolygon == 1) {
pointIsInPolygon = TRUE;
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Uw vuilnisdag" message:@"Op dinsdag 06:00 en vrijdag 06:00" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Stel notificatie in", nil];
//[alertView show];
} else {
UIAlertView *alertViewN = [[UIAlertView alloc]initWithTitle:@"Sorry" message:@"Geen ophaaldag gevonden in uw gebied" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
//[alertViewN show];
}
CGPathRelease(mpr);
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//this would be the yes button or cancel
if (buttonIndex == 0 ){
NSLog(@"ButtonIndex Okay");
}
}