Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

ios - CLLocation returning negative speed

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Usually, you get negative altitude and speed if the used location detection mechanism doesn't support it. For instance, when Wi-Fi or cell triangulation is used. Are you sure, you're getting GPS updates, so are you testing under the free sky? Even then, you'll still receive WiFi and cell tower location updates so you've to filter them out.

65 m horizontal accuracy is typical for Wi-Fi location updates.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...