【发布时间】:2012-08-24 09:12:56
【问题描述】:
如何计算设备上的实时速度?我用谷歌搜索了很多,但我得到的只是在完成旅程后计算距离和速度。我可以在运行时计算速度吗?
我们将不胜感激。
提前致谢。
【问题讨论】:
标签: ios real-time cllocationmanager
如何计算设备上的实时速度?我用谷歌搜索了很多,但我得到的只是在完成旅程后计算距离和速度。我可以在运行时计算速度吗?
我们将不胜感激。
提前致谢。
【问题讨论】:
标签: ios real-time cllocationmanager
这里CLLocationManager 类提供不同的位置字段,如纬度、经度、准确度和速度。
我使用CoreLocationController 所以对于位置更新我称之为波纹管方法
您可以在 - (void)locationUpdate:(CLLocation *)location 方法中获取当前速度,如下所示
- (void)locationUpdate:(CLLocation *)location
{
NSString *currentspeed = [NSString stringWithFormat:@"SPEED: %f", [location speed]];
}
否则下面是委托方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"in update to location");
NSString *currentspeed = [NSString stringWithFormat:@"SPEED: %f", [newLocation speed]];
}
您也可以从此链接获取示例... http://www.vellios.com/2010/08/16/core-location-gps-tutorial/
希望对你有帮助... :)
【讨论】:
有一个委托CLLocationManagerDelegate 将其添加到您的控制器头文件中。
初始化位置管理器并设置你为位置管理器实现委托方法的委托,像这样
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; // send loc updates to current class
你可以在你的类中编写方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"Location Speed: %f", newLocation.speed);
}
就是这样,您将通过上述方法以速度获取您的位置更新,并且它会尽可能频繁地触发。
【讨论】: