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?

mfaani
  • 33,269
  • 19
  • 164
  • 293
Camus
  • 827
  • 2
  • 20
  • 36
  • The region you're creating only has a 10m radius, are you sure you are entering and exiting that region? – Craig Oct 04 '12 at 08: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 04 '12 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 04 '12 at 20:39
  • No. I was just wondering whether was possible or not. Thank you – Camus Oct 04 '12 at 22:00

1 Answers1

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.

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
  • 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 04 '12 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. – Bill Burgess Oct 04 '12 at 23:21
  • Thanks. So you mean it is might be a good idea use some math instead of the delegates methods? – Camus Oct 04 '12 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. – Bill Burgess Oct 05 '12 at 00:40
  • Thanks man. I ask another question regarding the same matter. If you can have a look I would appreciate. – Camus Oct 05 '12 at 02:11
  • so if my iPhone app is using `didExitRegion` callback to do `startLocationUpdates` would the minimum be set to 100 or 450? – mfaani Jul 28 '17 at 14:38
  • The radius is always set to whatever you pick. If it doesn't meet Apple's minimum, they will increase it at the system level, you'll never know. Also, if I remember right, you can't call startLocationUpdates from the background, the app would need to be in the foreground. – Bill Burgess Jul 28 '17 at 20:15
  • @BillBurgess is it still the case that minimum radius should 100 meters? As I have set 30 meters, it's not working according to 30 meters, but it is working according to 100 meters. I don't understand it is due to tower cell, signal issue or Apple set 100 meters by default? – Amanpreet May 07 '18 at 13:25
  • We have told our customers to use 150m with a hard minimum set at 100m. They just don't seem as reliable at that size. It will save you some support if you prevent them from creating them as sizes under 100m. – Bill Burgess May 07 '18 at 16:34