ioscore-locationcllocationmanagerclregionregion-monitoring

Geofencing iOS 6


I am creating an app that tells the user whether they are near the destination. I am calculating the distance between the currentLocation and the destination. I'm doing the calculation inside the didUpdateLocations. It is working but I've seen that there are methods that can deal with that without the need of doing any math.

I am registering the region in the CLLocationManager; however it seems that the methods didExitRegion and didEnterRegion are not been called.

Here are the part of the code where I register the region:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.locationManager startUpdatingLocation];
    [self.mySearchBar resignFirstResponder];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    self.distToRemind = 0;

    [worldMap removeAnnotations:[worldMap annotations]];
    NSLog(@"executou de primeira");

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:[self.mySearchBar text] completionHandler:^(NSArray *placemarks, NSError *error)
         {
             CLPlacemark *placemark = [placemarks lastObject];

             //test
             //DefaultAnnotation *annot = [[DefaultAnnotation alloc] initWithCoordinate:placemark.location.coordinate andTitle:@""];
             CLRegion *newRegion = [[CLRegion alloc] initCircularRegionWithCenter:placemark.location.coordinate radius:10.0 identifier:@"RegionBoundary"];

             DefaultAnnotation *regionAnnotation = [[DefaultAnnotation alloc] initWithCoordinate:newRegion.center andTitle:@""];

             [self identifyPlacemark:placemark andSetAnnotation:regionAnnotation];

             MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(regionAnnotation.coordinate, 250, 250);


             [worldMap addAnnotation:regionAnnotation];
             [worldMap setRegion:region animated:YES];

             [self.locationManager startMonitoringForRegion:newRegion];

             if (self.boolPushButtonTapped) {
                  [self pushButtonTapped];
             }
         }
         ];
}

Am I doing something wrong here?


Solution

  • Ok, a few things to keep in mind when using the region monitoring ability in iOS.

    Your code looks good but is missing the logic surrounding your CLLocationManagerDelegate. Without a proper delegate to handle the callbacks, you are likely just missing the callbacks ( -didExitRegion/-didEnterRegion).

    In my experience, I create a singleton class to handle all of my location manager delegate methods. Make sure you sign up to listen for them. If you include some more code surrounding those delegate calls, I'd be glad to help you more. There are plenty of tutorials out there that should document how to set them up correctly. Good luck.

    *Note: I spoke to a location engineer at WWDC this year about a lot of these unknowns surrounding min region size and number of regions. I can confirm the min region size at 100, but not the max number of regions. I haven't had the need as of yet.