I want to make an app that gives me an Enter & Exit alert message for current position with 10 meters radius. Here is my code:
-(void)startLocationServices
{
if (self.locationManager == nil)
{
self.locationManager = [CLLocationManager new];
}
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager startUpdatingLocation];
}
-(void)stopLocationServices
{
[self.locationManager stopUpdatingLocation];
[self.locationManager setDelegate:nil];
//self.locationUpdateTextView.text = @"Location Service stop";
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self startLocationServices];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
self.locationUpdateTextView.text = [@"New Location: \n\n" stringByAppendingFormat:@"%@ \n \nCurrent Position\nLatitude: %f \nLongitude: %f", [locations lastObject], self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:10.0 identifier:@"Test"];
[self.locationManager startMonitoringForRegion:region];
[self stopLocationServices];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Status" message:@"You Enter the region" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alertView show];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Status" message:@"You Exit the region" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alertView show];
}
- (void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
self.availabilityTextView.text = [@"\nError:" stringByAppendingFormat:@"%@", error];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
I call [self stopLocationServices];
in didUpdateLocations
method so that it grabs the user's current position, set the radius and then stop. When I run the app and move 10 meters it is not giving any alert message though I cross the 10 meters radius. Rather, I get it randomly especially when I restart the app after crossing the 10 meters radius. I know the cell tower issue in this case, but I am not sure my thinking is accurately workable in this scenario. If anyone understands my problem and knows any solution please share that with me.