如何在ios中以终止状态在每个200米处获得位置

时间:2016-02-01 11:55:05

标签: ios location cllocationmanager suspend

我正在努力让用户的位置处于应用终止状态。我是在startMonitoringSignificantLocationChanges这样做的,但它在3公里后或5分钟后给出了位置。所以无法正确创建路线。请指导我如何做到这一点。

 if (_anotherLocationManager)
    [_anotherLocationManager stopMonitoringSignificantLocationChanges];

self.anotherLocationManager = [[CLLocationManager alloc]init];
_anotherLocationManager.delegate = self;
_anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

if(IS_OS_8_OR_LATER) {
    [_anotherLocationManager requestAlwaysAuthorization];
}
[_anotherLocationManager startMonitoringSignificantLocationChanges];

3 个答案:

答案 0 :(得分:0)

我自己解决了我的问题......

创建一个locationManager对象并像这样分配

 self.locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // setting the accuracy
[self.locationManager requestAlwaysAuthorization];

if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
    [self.locationManager setAllowsBackgroundLocationUpdates: YES];
}

self.locationManager.distanceFilter = 20.0;

[self.locationManager startMonitoringSignificantLocationChanges];

self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;

[self.locationManager startUpdatingLocation];

self.locationManager.pausesLocationUpdatesAutomatically = YES;

self.locationManager.delegate = self;

现在设置位置管理器委托方法。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
{
    if (newLocation.speed>7){
      // here you got location, i settled speed 7 for my convenience.  
    }

    if (newLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy) {
        //Desired location Found
        [self.locationManager stopUpdatingLocation ];
    }
}

你必须写这个stopUpdatingLocation,否则你的电池消耗会增加很多。

答案 1 :(得分:0)

使用地理围栏来达到同样的目的。

  • 创建一个半径为 200 米的地理围栏。
  • 默认情况下,notifyOnExit 为 true
  • 实现 LocationManager 的委托 didExitRegion
  • 对于终止状态,应用程序将在 didFinishLaunchingWithOptions 中使用 UIApplication.LaunchOptionsKey.location
  • 在位置启动键上创建一个位置管理器对象的实例,您可以获得该位置退出的区域,并在每个出口处继续创建 200 米的围栏。

答案 2 :(得分:-1)

您无法通过重要的位置监控获得200米的粒度或连续跟踪。

您是否在文档中看到了这些说明:

  

重要更改位置服务仅在何时提供更新   设备的位置发生了重大变化,例如   500米或以上。

     

如果GPS级别的准确度对您的应用并不重要,而您不需要   连续跟踪,您可以使用重要更改位置   服务。