Bri*_*kau 12 iphone objective-c accelerometer core-location
我需要实现一个原生iPhone应用程序来测量手机的速度(基本上是速度计).我知道你可以很容易地通过CoreLocation API这样做,但我担心电池消耗,因为这是一个可以一次使用长达几个小时的实时测量.我的理解是,当您主动监控来自LocationManager的事件时(即使我实际上并不关心GPS位置),它是电池密集型的.
探索的另一个明显选择是使用加速度计来尝试计算速度,但API中没有任何内容可以帮助您这样做.根据我的研究,应该可以做到这一点,但看起来非常复杂且容易出错.从加速到速度的转换可能很难开始,加上iPhone加速度计数据可能会"嘈杂".我熟悉使用低/高通滤波等的SDK示例 - 但我没有看到任何显示计算速度的好例子.
有没有人有他们可以分享的任何实际经验?代码会很棒,但实际上我只是想知道是否有人成功完成了这项工作(对于一个长期存在的应用程序)以及他们采取了什么方法.
编辑:我有一个使用LocationManager API的工作原型.它工作正常,但更新周期远非理想的速度实时测量.根据具体情况,有时可能需要4-5秒才能更新.以给定速度巡航往往可以正常工作,但是从用户交互的角度来看,加速/减速往往会非常严重.另外,我需要将速度提供给我正在进行的其他一些计算,而精度并不是我真正需要的.
它似乎可能基于(很少)我见过的其他应用程序,特别是gMeter声称不使用GPS但准确计算速度.我真的很惊讶没有任何参考或任何示例代码可以在任何我能找到的地方展示这一点.我意识到这很复杂,但肯定有一些东西在那里.
ben*_*ord 10
从实用的角度来看,你不会从作用在加速度计上的力获得准确的速度.
使用GPS,每隔1分钟读取一次读数,并让GPS在两者之间进入睡眠状态.
这是一个例子:
SpeedViewController.h
CLLocationManager *locManager;
CLLocationSpeed speed;
NSTimer *timer;
@property (nonantomic,retain) NSTimer *timer;
Run Code Online (Sandbox Code Playgroud)
SpeedViewController.m
#define kRequiredAccuracy 500.0 //meters
#define kMaxAge 10.0 //seconds
- (void)startReadingLocation {
[locManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval ageInSeconds = [newLocation.timestamp timeIntervalSinceNow];
//ensure you have an accurate and non-cached reading
if( newLocation.horizontalAccuracy > kRequiredAccuracy || fabs(ageInSeconds) > kMaxAge )
return;
//get current speed
currentSpeed = newLocation.speed;
//this puts the GPS to sleep, saving power
[locManager stopUpdatingLocation];
//timer fires after 60 seconds, then stops
self.timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(timeIntervalEnded:) userInfo:nil repeats:NO];
}
//this is a wrapper method to fit the required selector signature
- (void)timeIntervalEnded:(NSTimer*)timer {
[self startReadingLocation];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13061 次 |
| 最近记录: |