CLLocationManager设置更新速度

时间:2014-02-04 21:00:36

标签: ios cllocationmanager

我正在使用CLLocationManager来获取用户的X / Y线。代表会每隔一秒左右触发一次,但我只需要每隔15秒钟左右来管理我的API。有没有办法减慢它以节省电池电量?每次停止/启动它会更好吗?

2 个答案:

答案 0 :(得分:0)

我的一个应用程序遇到了同样的问题。我需要将用户位置发送到后端。 我解决这个问题的诀窍。

1-我首先设置了一个全球位置(纬度和经度两个字符串),然后在首次启动“didUpdateToLocation”时初始化。

2-下次当didUpdateToLocation调用时,我检查了它的前一个位置还是一个新位置。如果新位置然后只更新发送到后端。

3-主要技巧是这个

NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
 if (locationAge > 5.0) return;

它会给我一个调用位置更新的时间间隔。

这是代码(可能有点旧)

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

{

// test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
// test the age of the location measurement to determine if the measurement is cached
// in most cases you will not want to rely on cached measurements
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
// store all of the measurements, just so we can see what kind of data we might receive

NSString *tempLat = [NSString stringWithFormat:@"%.4f", [newLocation coordinate].latitude];
NSString *tempLong = [NSString stringWithFormat:@"%.4f", [newLocation coordinate].longitude];

//NSLog(@"lat--%@\nlong--%@",tempLat,tempLong);
////check if location matches with currenlty saved location
if (![tempLat isEqualToString:self.latitude] && ![tempLong isEqualToString:self.longitude]) 
{
 //call to webservice  
}

}

希望它会帮助你!!!

答案 1 :(得分:0)

只要A)它获得更准确的修复,位置管理器就会发射,只要它看起来合适.B)它会看到超出距离滤波器设置的移动。您可以使用pausesLocationUpdatesAutomatically使其在实际操作时退出查找。

在你身边,采取负责任的行动。您应该测试每个事件,如果它太近,不准确或没有足够的变化以保证反应然后忽略。最好的情况是,您将停止更新并设置为区域监控或在此时唤醒重要的位置更改。如果您需要随时进行持续更新,那么您将无法获得多种选择。

相关问题