您的位置:首页 > 其它

iphone获取当前移动速度

2012-02-11 23:22 393 查看
有时候我们需要获取iphone当前的移动速度,比如导航软件。 虽然无法有直接的api, 但是我们可以使用定位功能中的某些接口和参数来算出当前的速度。 具体方法如下:

1. 先启动定位功能

self.locManager = [[[CLLocationManager alloc] init] autorelease];
if (!self.locManager.locationServicesEnabled)
//检测是否开启定位服务
{
return;
}

self.locManager.delegate = self;
self.locManager.desiredAccuracy = kCLLocationAccuracyBest; //设置最高的精度
[self.locManager startUpdatingLocation]; //开始定位

2. 根据回调参数和数据计算速度

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (newLocation.horizontalAccuracy < kCLLocationAccuracyHundredMeters)
{
if (self.lastAccurateLocation)
{
NSTimeInterval dTime = [newLocation.timestamp timeIntervalSinceDate:self.lastAccurateLocation.timestamp];
//计算和上次的时间差
float distance = [newLocation getDistanceFrom:lastAccurateLocation]; // 获取相比上次,已经移动的距离
if (distance < 1.0f) return;

aggregateDistance += distance;

float speed = 2.23693629 * distance / dTime; //计算出速度
NSString *reportString = [NSString stringWithFormat:@"Speed:
%0.1f miles per hour. Distance: %0.1f meters.", 2.23693629 * distance / dTime, aggregateDistance];

}
self.lastAccurateLocation = newLocation;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: