我正在开发一个新的应用程序,该应用程序使用coreLocation和mapkit框架。
我的问题是尝试获取当前速度始终会返回负值。我带着iPhone到有良好3G信号的地方,但无论如何,location.speed的值总是-1。
以下是相关代码:
#define kRequiredAccuracy 1500.0 //meters
#define kMaxAge 60.0 //seconds
在init方法中:
self.locationManager=[[CLLocationManager alloc] init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
然后didUpdateToLocation:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSTimeInterval ageInSeconds = [newLocation.timestamp timeIntervalSinceNow];
NSLog(@"Location: %@", [newLocation description]);
if( newLocation.horizontalAccuracy > kRequiredAccuracy || fabs(ageInSeconds) > kMaxAge )
{
NSLog(@"inacurate position");
[self.delegate inacuratePosition];
}
else {
[self.delegate locationUpdate:newLocation andOldLocation:oldLocation];
location=newLocation.coordinate;
}
if(tracking)
{
[self updatePosition];
}
if(firstTime)
{
[self placeStartMark];
firstTime=FALSE;
}
}
最后,在我实现协议的视图控制器中:
- (void)locationUpdate:(CLLocation *)newLocation andOldLocation:(CLLocation*)oldLocation{
double speed = [newLocation speed] *3.6;
double altitude= [newLocation altitude];
[self checkMaxSpeedAndAltitude:speed :altitude];
if(speed< 0)
speed=0;
locationLabel.text=[NSString stringWithFormat:@"speed: %f km/h altitude: %f m", speed,altitude];
}
我快要疯了,如果有人知道任何解决方案,那肯定会很有帮助。谢谢。