Question

I'm trying to implement a function of a Geo location for a user, I'm statically setting up latitude and longitude information, when app starts if the user is within that area I'm showing up a message that "You've been reached to office" else "You're going out from office". I've implemented below code to achieve this, I tried by moving all around by steps and on vehicles, but in both the cases it always shows that "You've been reached to office", however I was 2km away from that location! I think the problem is in the comparison of Geo data in CLLocationManager delegate.

- (void) startUpdateUserLocation
{
    if(!locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

//    [locationManager startUpdatingLocation];

    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(latitude, longitude);

    CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:coord radius:kCLDistanceFilterNone identifier:@"identifier"];
    [locationManager startMonitoringForRegion:region];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    latitude = 23.076289;
    longitude = 72.508129;
}

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    MKCoordinateRegion region;
    region.center = mapView.userLocation.coordinate;
    region.span = MKCoordinateSpanMake(0.25, 0.25);
    region = [mapView regionThatFits:region];
    [mapView setRegion:region animated:YES];

    lblCurrentCoords.text = [NSString stringWithFormat:@"lat %f lon %f",mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude];

    [self startUpdateUserLocation];
}

- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
    [listOfPoints addObject:manager.location];
    [tablePoints reloadData];

    /*
     *  locationManager:didEnterRegion:
     *
     *  Discussion:
     *    Invoked when the user enters a monitored region.  This callback will be invoked for every allocated
     *    CLLocationManager instance with a non-nil delegate that implements this method.
     */

    lblLocationStatus.text = @"You're in office area!...";
}

- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
    /*
     *  locationManager:didExitRegion:
     *
     *  Discussion:
     *    Invoked when the user exits a monitored region.  This callback will be invoked for every allocated
     *    CLLocationManager instance with a non-nil delegate that implements this method.
     */

    lblLocationStatus.text = @"You're going out from office area!...";
}

- (void)locationManager:(CLLocationManager *)manager
didStartMonitoringForRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_TBD,__IPHONE_5_0)
{
    /*
     *  locationManager:didStartMonitoringForRegion:
     *
     *  Discussion:
     *    Invoked when a monitoring for a region started successfully.
     */

    lblLocationStatus.text = @"Start monitoring...";
}

- (void)locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
    /*
     *  locationManager:monitoringDidFailForRegion:withError:
     *
     *  Discussion:
     *    Invoked when a region monitoring error has occurred. Error types are defined in "CLError.h".
     */

    lblLocationStatus.text = @"Stop monitoring...";
}

I am trying to accomplish the following things!

  1. If the user entered into the Geo location, he should be "alert". --- How to match location?
  2. If moving around within that Geo location then the code should be monitoring this activity! --- Need to set a desired accuracy property?
  3. I want my code to check constantly for the user Geo location, how do I do that? --- Need to call function in NSTimer ?

I found many questions on SO asked about the same but NO one has matched answers! Someone please guide me whether I'm going in the right direction or not as this code doesn't show up! :)

Was it helpful?

Solution

It sounds like you should be using region monitoring instead, which tells you when the user enters or exits a circular area. Set it up with startMonitoringForRegion: and implement the CLLocationManagerDelegate methods

– locationManager:didEnterRegion:
– locationManager:didExitRegion:
– locationManager:monitoringDidFailForRegion:withError:
– locationManager:didStartMonitoringForRegion:

If you're having trouble with bad location data coming in, check for the age of the CLLocation in locationManager:didUpdateLocations: or locationManager:didUpdateToLocation:fromLocation:. If it's more than 60 seconds old, don't use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top