objective-c - 未调用 CoreLocation 区域代表

标签 objective-c ios gps core-location

我正在构建一个应用程序,特别是一个基于位置的应用程序,所以我需要让用户坐标经度和纬度及其工作。

现在我需要知道用户是否输入了我使用 CLLocationManager 预定义的区域。代表,所以我覆盖 didEnterRegionNSLog语句来知道用户是否进入了区域。


-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 

我调用 startMonitoringForRegion:但是像 didEnterRegion 这样的代表没有任何 react 或 monitoringDidFailForRegionCLRegion 的任何代表.

这是我的一些代码:
   -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
   {
        locLongittude=newLocation.coordinate.longitude;
        locLattiude=newLocation.coordinate.latitude;

        self.regionTimer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(monitorRegion) userInfo:nil repeats:NO];
   }

   -(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)radius
   {
      CLLocationCoordinate2D home;
      home.latitude = latitude;
      home.longitude = longitude;

      CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:radius identifier:@"home"];

      if([CLLocationManager regionMonitoringEnabled] && [CLLocationManager regionMonitoringAvailable])
          [locManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest]; 

      NSLog(@"Count: %d region aval %d enable %d",[[locManager monitoredRegions]count], [CLLocationManager regionMonitoringAvailable],[CLLocationManager regionMonitoringEnabled]);
      for( CLRegion* region in locManager.monitoredRegions )
      NSLog( @"%@", region );
   }

   -(void)monitorRegion
   {
      [self startMonitor:31.971160 longitude:35.832465 radius:10000.0];
   }

编辑:使用模拟器完成所有测试并使用 Product-> Debug -> Simulate Location 模拟位置

好的,伙计们,它有效。上面的代码效果很好,问题是要使 didEnterRegion调用方法是在您需要输入的区域之外,然后输入它以使其工作。

我所做的是在模拟中,我总是在我需要输入的区域内选择一个坐标,这就是它必须在该区域之外的问题。

所以这是我的代码与您分享

更新:我想知道为什么当我离开该区域时没有调用 didExitRegion ?

最佳答案

根据我自己的经验,Region Monitoring 似乎依赖于 Cell Change 事件(为什么 Apple 会这样设计?我想是为了延长电池生命周期)。所以,这个实现并不理想。

理想情况下,只要您越过边界,它就应该可以工作,但在 iPhone 上却不会以同样的方式发生。即使使用内置的提醒应用程序,有时它也会通过基于位置的提醒在附近失败。

因此,在您的情况下,我认为单元格更改事件不会发生,这就是为什么不调用 didExitRegion 的原因。您可以使用 startMonitoringSignificantLocationChanges 仔细检查它。

关于objective-c - 未调用 CoreLocation 区域代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10894600/

相关文章:

objective-c - 如何创建一个方法来返回另一个类的对象的数组?

iphone - 在以下情况下保存应用程序状态数据的最佳方法是什么?

objective-c - 删除用于提交到服务器的 NSDictionary Params 中的重复代码

ios - STPopUpController 将点击的数据传递给底部表单样式 iOS

c# - 从 .Net Winform 应用程序访问 GPS 数据

ios - 如何在 UITableView 中动画更改顺序?

iphone - 将数据存储到 NSUserDefaults

ios - iOS 2D 游戏的图形

java - 如何将 GPS 设备与 Android 应用程序连接

android - 应用程序在后台运行时无法找到位置更新