Remove CLBeaconRegion from monitoring depending on current location

558 Views Asked by At

So I was looking for a way to remove a BeaconRegion from the monitored regions depending on how far it is from my current location. I though I could use the center property, though I think I'm missing something... Because the latitude/longitude values are like not valid...

po [region center]
(latitude = 0.0000000000000000000000000000000000000000000000000000000033891907065820605, longitude = 0.000000000000000000000000000000000000059293723668713039) 

How can I remove a BeaconRegion depending con currentLocation?

2

There are 2 best solutions below

0
On BEST ANSWER

a CLBeaconRegion represents N bluetooth beacons with the same uuid. an ibeacon has no gps coordinate. it has an RSSI value (the signal strength) and a 'calculated' proximity property but no location.

same as a wifi router ;) there are services to associate beacons/wifi routers with a GPS location but it isn't standard. how should the beacon know? :)


On IOS, the CLBeaconRegion class only has a center property because it is a subclass of CLRegion

if YOU know the GPS locations of your regions use that data + your device's location

0
On

Yes, you can stop monitoring CLBeaconRegions depending on the user's location. But as you've discovered, the center property of this object won't help you do that (See @Daij-Djan's answer for an explanation as to why.)

The typical way to do you want is to set up to receive significant location changes using CLLocationManager at the same time as you set up beacon monitoring, like this:

[locationManager startMonitoringSignificantLocationChanges];

You then add a method like below to the delegate of your CLLocationManager to get callbacks each time the user changes locations significantly:

- (void)locationManager:(CLLocationManager *)locationManager
      didUpdateLocations:(NSArray *)locations {
   CLLocation* location = [locations lastObject];
   NSLog(@"latitude %+.6f, longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
   // TODO: change the monitored beacon regions depending on the
   // location.coordinate.latitude and location.coordinate.longitude     
   }
}

Note that you also need to make sure location services are authorized for your app for this to work and put a string corresponding to the NSLocationAlwaysUsageDescription key in your plist, but it is the same check you need to do to monitor for beacons anyway:

if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [locationManager requestAlwaysAuthorization];
}

See here for more details on signficant location changes: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html