NSLog detect updating current location in MKPolygon

446 Views Asked by At

noob here. I've read up on how to detect a point in a MKPolygon but I'm having difficulties implementing it -- it might just have to do with the part in the code at which I'm checking the user location… I'm not even sure if it's consistently checking. Anyway, here's the relevant code:

- (void)locationManager:(CLLocationManager *)manager
  didUpdateLocations:(NSArray *)locations {
self.current = [locations lastObject];
NSLog(@"lat%f - lon%f", self.current.coordinate.latitude, self.current.coordinate.longitude);
NSLog(@"%@", self.mapCIIP); }

and later

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay{
if([overlay isKindOfClass:[MKPolygon class]]){
    MKPolygonRenderer *view = [[MKPolygonRenderer alloc] initWithOverlay:overlay];
    view.lineWidth=1;
    view.strokeColor=[UIColor blueColor];
    view.fillColor=[[UIColor blueColor] colorWithAlphaComponent:0.5];


    return view;
    CLLocationCoordinate2D mapCoordinate = CLLocationCoordinate2DMake(self.current.coordinate.latitude, self.current.coordinate.longitude);

    MKMapPoint mapPoint = MKMapPointForCoordinate(mapCoordinate);
    if (CLLocationCoordinate2DIsValid(mapCoordinate)) {
        NSLog(@"Coordinate valid");
    } else {
        NSLog(@"Coordinate invalid");
    }

    CGPoint polygonViewPoint = [view pointForMapPoint:mapPoint];


    if ( CGPathContainsPoint(view.path, NULL, polygonViewPoint, NO) ) {
        self.mapCIIP = @"TRUE";
    }
    else {
        self.mapCIIP = @"false";
    };


}
return nil; }

I simulated different locations in Xcode with it running on my iPhone, and while the lat/long update, it's not giving me what I want for the true/false in the polygon I drew. At one point it was showing up as all true but now it's all (null) in NSLog. Thanks!

1

There are 1 best solutions below

2
On

Part way through your function it returns the view, so the code setting the mapCIIP can't be reached.