Pregunta

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");
    }
} 
¿Fue útil?

Solución

You must set the alert view's delegate to self when you create it, and implement the delegate method that gets executed when the alert is dismissed. Also keep a reference to your alert view after presenting it.

In the alert view delegate method (after the user dismisses the alert view), reset the reference to nil. When your condition to show an alert view is met, show it only if the reference equals nil.

EDIT In your particular case:

Where it says:

if (pointIsInPolygon == 1){
    // ...
}

Instead of showing the alert, set a flag to true. After you looped through all the eight elements, show only 1 alert if the flag is true.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top