5

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?

4
  • The region you're creating only has a 10m radius, are you sure you are entering and exiting that region?
    – Craig
    Oct 4, 2012 at 8:00
  • I just changed to 200 and it worked, although it takes 20m or more to send me an alert. How can I set small number. When I set 20m I was in the region but I walked more than 200m and I didnt get the alert when I left the region.
    – Camus
    Oct 4, 2012 at 13:26
  • Maybe you could set a region of 20m to then trigger the app to start using didUpdateLocations. I'm not sure you can rely on the GPS to be so accurate that it knows exactly when you're within 10m of a target. Are the users looking for something so small they can't see if they are there when they are 10m away?
    – Craig
    Oct 4, 2012 at 20:39
  • No. I was just wondering whether was possible or not. Thank you
    – Camus
    Oct 4, 2012 at 22:00

1 Answer 1

12

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

  • Regions are going to default to the minimum size no matter what you set the initial radius to. An Apple engineer told me it is 100M for GPS enabled devices. 450M for Wifi only devices that support region monitoring (iPad 3 and new iPod Touches)
  • Regions you can monitor are a limited commodity. The total number that can be monitored on a single device is limited. Again, an Apple engineer told me it is around 100 regions. Use the delegate methods to make sure your region was added good or bad.
  • Regions are very useful and have minimal impact on battery life. They also get their own location icon in the status bar. (hollow purple location arrow)
  • They work very much like every other location API, you need to respond to the delegate methods correctly to interpret the actions that are happening.

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.

9
  • Thanks for the detailed explanation. In my case, this is my first app, I will only set one region per time. I implemented the methods didExitRegion and didEnterRegion within the mapViewController, maybe it is cleaner to create an separate class to handle this. I set 200m and it is working, not very precisely though. I was stabilising the region by myself in the didUpdateLocation, doing some math and it was working very precisely. Do you think it is a bad idea to do that?
    – Camus
    Oct 4, 2012 at 22:00
  • It depends on your use case. The more precise the location you have, the better your accuracy will be in real world application. Could be just how the location is pulled out and how detailed you are in your simulator. I think I use 5 digits of precision for the long/lat in mine and I rarely have issues when testing. Glad you are up and going. Oct 4, 2012 at 23:21
  • Thanks. So you mean it is might be a good idea use some math instead of the delegates methods?
    – Camus
    Oct 4, 2012 at 23:27
  • I'm not sure what you need the math for. You can if you feel you need to. The delegate methods for the region monitoring are only going to give you updates when you cross the region boundary. What you do after that is up to you. Oct 5, 2012 at 0:40
  • Thanks man. I ask another question regarding the same matter. If you can have a look I would appreciate.
    – Camus
    Oct 5, 2012 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.