3

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.

bneely
  • 9,083
  • 4
  • 38
  • 46
Tulon
  • 4,011
  • 6
  • 36
  • 56
  • Hi @tTulon even i got same issue have cleared this issue.please let me know with updated code – sabir Feb 27 '14 at 05:20
  • You can create a CLCircularRegion and use startMonitoringForRegion of location manager. When you enter / exit the region delegate didEnterRegion and didExitRegion will be called. – Ken Cheung Jun 26 '14 at 09:55
  • @KenCheung Thanks for commenting. I already got it. Have a nice day. – Tulon Jun 26 '14 at 10:26
  • What phone are you using? – Rod Jul 24 '14 at 05:01
  • You shouldn't call stopUpdatingLocation. How would your app know your current location in relation to your region? – Rod Jul 24 '14 at 05:11

0 Answers0