Я пытаюсь использовать регионы мониторинга, чтобы отслеживать, посещали ли пользователи ориентиры.
менеджер местоположений инициализируется в viewcontroller вместе с mapkit
в viewdidload контроллера вида:
if (self.locationManager == nil)
{
// NSLog(@"creating location manager");
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = kCLDistanceFilterNone;
}
NSSet* set=[locationManager monitoredRegions];
if ([CLLocationManager regionMonitoringAvailable] && [CLLocationManager regionMonitoringEnabled]) {
NSLog(@"region monitoring okay");
NSLog(@"monitored regions: %@",set);
}
Я получаю NSLogs "Мониторинг региона в порядке" и все регионы правильно.
добавление регионов сделано так
double metres=20.0;
CLLocationDistance dist=metres;
CLLocationAccuracy acc=1.0;
CLRegion *reg=[[CLRegion alloc] initCircularRegionWithCenter:coordinate radius:dist identifier:landmarkObj.landmarkName];
[locationManager startMonitoringForRegion:reg desiredAccuracy:acc];
но обратные вызовы не срабатывают
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Entered"
message:region.identifier
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exited"
message:region.identifier
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
NSLog(@"started monitring for region: %@",region);
}
- (void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
NSLog(@"%@",error);
}
обновление местоположения, однако, работает нормально.
[locationManager startUpdatingLocation];
запускает обратный вызов didUpdateToLocation, как и ожидалось
Обновление: вместо него используется didUpdatToLocation для мониторинга регионов.
все еще интересно узнать, почему это не сработает, похоже, мало кто добился успеха в области мониторинга региона