4

In my application I want do geofencing. I have list of locations, for which I am setting geofencing regions. All location have radius 100m. Following is the code to set Region:

CLLocationCoordinate2D cord;
cord.latitude = [location.latitude doubleValue];
cord.longitude = [location.longtitude doubleValue];
CLLocationDistance regionRadius = location.radius;
CLRegion *region  = [[CLRegion alloc] initCircularRegionWithCenter:cord radius:regionRadius identifier:[NSString stringWithFormat:@"%d",location.locId]];
[appDelegate.locationManager startMonitoringForRegion:region];

My problem is that, its not working properly. For debugging, in didEnterRegion delegate added distance calculation in between current location and region location some times this distance is more then 1500m.

Following is the code to calculate distance between current location and region location:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    CLLocation *loc1 = locationManager.location;
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:region.center.latitude longitude:region.center.longitude];
    double dist = [loc1 distanceFromLocation:loc2];
}

Any idea, why -locationManager:didEnterRegion: get triggered such a wrong way?

OnkarK
  • 3,745
  • 2
  • 26
  • 33

1 Answers1

3

This is a glitch in iOS Region Monitoring and I have faced the similar issue with Geofencing and I have found a way to solve that problem.

I have posted the question here and I have answered myself, I have a blog post on that as well:-

  1. Region Monitoring Glitch on iOS 7 - Multiple Notifications at the same time
  2. iOS Region Monitoring and Location Manager
Community
  • 1
  • 1
Ricky
  • 10,485
  • 6
  • 36
  • 49
  • 1
    Thanks for answer Ricky. As you suggested to check current location & Region, I did that with distance calculation. Have u found any reason for this wrong behaviour of location manager? – OnkarK May 20 '14 at 09:31
  • 1
    No problem. You may use **[theRegion containsCoordinate:theLocationCoordinate];** to double confirm if it is really inside the region before triggering the event. I have searched up and down in various websites, I couldn't find anything. I believe it is probably due to bad location with big radius (accuracy) that might happen to a certain countries. I can not confirm on this tough. – Ricky May 20 '14 at 09:37