我正在开始一个使用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];
}
我变得疯狂,所以如果有人知道任何解决方案,它肯定会有所帮助。 谢谢