I'm starting a new app which uses the coreLocation and the mapkit frameworks.
My problem is trying to get the current speed always returns me a negative value, i have take my iPhone with me to places with a good 3g signal and it doesn't matter, location.speed value is always -1.
here is the code which matters:
#define kRequiredAccuracy 1500.0 //meters
#define kMaxAge 60.0 //seconds
in the init method:
self.locationManager=[[CLLocationManager alloc] init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
then 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;
}
}
and finally in the view controller in which I'm implementing the protocol:
- (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];
}
Im getting crazy so if anyone knows any solution it will be helpful for sure.
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…