ios - 如何在ios中以终止状态获取每200米的位置

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

最佳答案

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

创建一个 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;

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

- (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,否则你的电池消耗会增加这么高。

关于ios - 如何在ios中以终止状态获取每200米的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35129918/

相关文章:

ios - 在后台从 BLE 读取数据

iphone - 针对 iOS 3 和 iOS 4 启用了 locationServices

swift - 如何在mapbox-gl中显示用户位置标记?

ios - NSInvocation 返回 nil

ios - 在 UIScrollView 中绘制 UIImageView

location.href 的 JavaScript 问题

java - 如何在已动态添加的JLabel 的JPanel 中获取Location()?

android - 将 Google map 缩放到当前位置

ios - 更改 NSDate 工作日和月份的语言

ios - 每次在 CoreData 中添加新对象时如何更新属性?