开发者

iPhone GPS speed calculation issue

In my application, I am using LocationManager for getting location, also I want calculate DISTANCE, SPEED and TIME.

From Location manager I am getting time and distance successfully,But when I calculate the speed from coordinates it is showing wrong values.

For getting location I am using following code.

CLLocationManager *locManager ;
float fltDistanceTravelled; 
double calculatedSpeed;

//===============
locManager=[[CLLocationManager alloc] init];
locManager.delegate=self;
locManager.distanceFilter=25.0f;
locManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
[locManager startUpdatingLocation];
//=====================

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    if(newLocation && oldLocation)
    {
        // getting distance
        fltDistanceTravelled +=[self getDistanceInKm:newLocation fromLocation:oldLocation];
    }
    if(oldLocation != nil)         
    {
        CLLocationDistance distanceChange = [newLocation getDistance开发者_JS百科From:oldLocation];
        NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
        // calculate speed
        calculatedSpeed= (distanceChange / sinceLastUpdate) * 3.6;       
    }
}

but this code return wrong values of calculatedSpeed (speed) of the car.


You are already getting the speed data in the delegate method.

CLLocation has a speed property

Rather than try and calculate it yourself, just get it from newLocation.speed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜