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;
}
}
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;
}
}
相关文章推荐
- iPhone开发--获取当前时间与2001年1月1日的时间差以毫秒为单位
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- 使用SENS API获取当前网卡连接速度
- iPhone之获取当前位置
- iphone获取当前时间
- UWP开发:获取用户当前所在的网络环境(WiFi、移动网络、LAN…)
- 获取iphone本机号码(在iOS 6.1, iPhone5 ,移动号码, xcode 4.6上测试通过)
- iphone开发--获取当前app的名称和版本号
- 如何获取iPhone电池的状态和当前的电量
- iPhone设备当前IP和SSID的获取
- 获取当前移动设备分辨率及DPI
- 如何获取当前系统时间 iphone开发 ios编程
- 获取当前iphone设备的流量
- 微信小程序实例-获取当前的地理位置、速度
- 需求:获取iPhone当前Wi-Fi的SSID等网络信息
- 获取当前电脑的运行速度
- android5.1.1 获取当前移动信号强度
- iphone开发--获取当前app的名称和版本号
- iPhone中如何获取当前输入法