iphone CLLocationmanager Region monitoring callbacks not triggered

…衆ロ難τιáo~ 提交于 2019-12-21 04:46:06

问题


I am trying to use monitoring regions to track if users have visited landmarks. the location manager is initialized in a viewcontroller along with a mapkit

in viewdidload of the view controller:

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);
} 

i get the NSLogs "region monitoring okay" and all the regions correctly.

adding of the regions are done like so

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];

but the callbacks are all not triggered

 - (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);
}

updating the location however, works fine.

[locationManager startUpdatingLocation];

triggers the callback didUpdateToLocation as expected

Update: used didUpdatToLocation to monitor for regions instead. still interested to know why this would not work though, looks like few have had success with region monitoring


回答1:


the region tracking stuff is for low granularity position tracking and is triggered on cell location boundaries, so if you don't cross a cell boundary, you will never get your regions checked. I had the same issues and researched this and a different website had a comment about this which pointed to this apple forum:

https://devforums.apple.com/message/251046#251046

If you read all the comments you will understand why this is not working.

I am trying a work around where I define my own NSSets to contain tracked CLRegions and occupied CLRegions and then when I get a locationManager:didUpdateToLocation:fromLocation: callback, I check all the regions in my tracked set to see if I was NOT in the inRegions set but now am in the tracked region (add to inRegions and call back with enterRegion) or if I was inRegion but now am not (remove from inRegions and call back with exitRegion). It is a work in progress now.




回答2:


did you set CLLocationManagerDelegate to your ViewController?

@interface Maps : UIViewController <CLLocationManagerDelegate>{  
...
}



回答3:


What are you putting for your center coordinate? That part is missing from your example. I have seen other examples where they are are not filling it with a precise enough coordinate combined with the radius of the region for it to ever trigger. You have to have a pretty precise coordinate entered (probably 5 or 6 decimals) if you want to trigger at a 20 meter radius.

Everything else looks pretty good.




回答4:


Try use the Debug -> Location -> Freeway drive for simulation, I have a similiar problem, but I created a few regions and the didEnterRegion is trigged with more than 1.000 meters... I don't know Way.. I have set:

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



回答5:


You also need NSLocationAlwaysUsageDescription in your plist, and need to call [locationManager requestAlwayAuthorization] in your app. NSLocationWhenInUseUsageDescription will turn off region monitoring.



来源:https://stackoverflow.com/questions/8586803/iphone-cllocationmanager-region-monitoring-callbacks-not-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!