[英]Methods LocationManager:didEnterRegion and LocationManager:didExitRegion is getting called continuously
[英]didEnterRegion of LocationManager not called ever
我厭倦了發現這個問題,但無法理解為什么會發生這種情況,因為位置管理器的委托方法didEnterRegion
或didExitRegion
被調用過。 我在我的 iPhone 上對此進行了測試,但它不起作用。 請告訴我我錯過了什么。 我的源代碼是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; manager = [[CLLocationManager alloc] init]; manager.delegate = self; dist = 100; coord = CLLocationCoordinate2DMake(24.004686, 74.554088); region=[[CLRegion alloc] initCircularRegionWithCenter:coord radius:dist identifier:@"emanuele"]; [manager startMonitoringForRegion:region desiredAccuracy:10]; return YES; } - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{ NSLog(@"Enter Region."); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Entered the Location." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; // [alert release]; } - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { NSLog(@"didExitRegion"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Exited the Location." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; // [alert release]; }
任何人都可以弄清楚出了什么問題嗎? 我搜索了很多演示和源代碼,但其中大部分都以didUpdateToLocation
,在我的案例中調用了它,但我對didEnter
和didExit
有疑問。 我也實現了didFailWithError
但它沒有做這件事。 請給出為什么不調用這兩種方法的任何線索。 或者給我任何調用這些方法的鏈接,我沒有找到這些方法的示例/源代碼。 任何幫助將不勝感激。
您的頭文件是否聲明為 CLLocationManagerDelegate?
一旦你設置了監控區域,你應該通過刪除該代碼元素進行測試,然后在啟動時檢查 UIApplicationLaunchOptionsLocationKey 鍵,如下所示:
if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]){
NSLog(@"UIApplicationLaunchOptionsLocationKey - fired");
if(locman == nil){
locman = [[CLLocationManager alloc]init];
locman.delegate = self;
locman.distanceFilter = kCLLocationAccuracyBest;
}
}
然后將調用位置管理器委托,並且 didEnter 和 didExit 應該正常觸發。
在viewDidLoad
中,添加以下代碼
self.beaconRegion.notifyOnEntry=YES;
self.beaconRegion.notifyOnExit=YES;
self.beaconRegion.notifyEntryStateOnDisplay=YES;
[self.locationManager startMonitoringForRegion:self.beaconRegion];
[self.locationManager requestStateForRegion:self.beaconRegion];
我希望這會解決你的問題。
只有在常規設置中啟用后台應用程序刷新后,我才能獲得 didEnterRegion 和 didExitRegion。
您是否也嘗試過didDetermineState
? didEnterRegion
和didExitRegion
僅在進入/退出時調用,但根據我的理解,如果您在該地區並且實際上沒有移動, didDetermineState
會告訴您您當前是否在該地區。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.