![](/img/trans.png)
[英]calculate speed using corelocation framework is showing in simulator not in device-ios8
[英]how to calculate speed in ios 7 using coreLocation framework
在iOS 7之前我用来计算速度如下
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
double speed = newLocation.speed;
NSLog(@"Speed of Device is %f",newLocation.speed);
// manual method
if(oldLocation != nil)
{
CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
NSTimeInterval sinceLastUpdate = [newLocation.timestamp
timeIntervalSinceDate:oldLocation.timestamp];
double calculatedSpeed = distanceChange / sinceLastUpdate;
NSLog(@"Speed of Device is %f",calculatedSpeed);
}
}
因为这个方法已被弃用,。 请建议我使用CoreLocation使用iOS7计算速度的另一种方法。
您可以执行以下操作:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *loc = locations.lastObject;
double speed = loc.speed;
NSLog(@"%f", speed);
}
第一种方法 :保存旧呼叫以前的呼叫。 。更准确的kCLLocationAccuracyBestForNavigation
。 在某些情况下,这可能会给您不准确的结果。(例如停止或错过报告一个位置等)。
CLLocationDistance distanceChange = [newLocation distanceFromLocation:oldLocation];
NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
double calculatedSpeed = distanceChange / sinceLastUpdate;
NSLog(@"calculatedSpeed using old location:%.1f",calculatedSpeed);
第二种方法是使用速度属性。使用此属性,您将获得车辆的当前速度(移动速度)。它将以m / s为单位给出速度 。 要将其转换为km / hr使用
location.speed * 3.6
使用kCLLocationAccuracyBestForNavigation
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = locations.lastObject;
double speed = location.speed*3.6;
NSLog(@"%f", speed);
}
“getDistanceFrom:”方法简单地重命名为“distanceFromLocation:” - 您可以使用它。
CLLocationDistance distanceChange = [newLocation distanceFromLocation:oldLocation];
其他解决方案以km / h为单位。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [self.locationManager location];
double speedCalculada = location.speed * 3.6;
self.lblVelocidad.text = (location.speed<0)?@"-- km/h":[NSString stringWithFormat:@"%d Km/h", (int) speedCalculada];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.