我使用以下代码计算设备从一个地方移动到另一个地方的距离。是否正确请看我的代码。
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
NSDate *now = [NSDate date];
NSTimeInterval intervalDate = [now timeIntervalSinceDate:now_prev];
sx = acceleration.x * kFilteringFactor + sx * (1.0 - kFilteringFactor);
sy = acceleration.y * kFilteringFactor + sy * (1.0 - kFilteringFactor);
sz = acceleration.z * kFilteringFactor + sz * (1.0 - kFilteringFactor);
[xLabel setText:[NSString stringWithFormat:@"%.2f",sx]];
[yLabel setText:[NSString stringWithFormat:@"%.2f",sy]];
[zLabel setText:[NSString stringWithFormat:@"%.2f",sz]];
float aValue = sqrtf(sx*sx+sy*sy+sz*sz);
[gLabel setText:[NSString stringWithFormat:@"%.2f g",aValue]];
velX += (sx * intervalDate);
distX += (velX * intervalDate);
velY += (sy * intervalDate);
distY += (velY * intervalDate);
velZ += (sz * intervalDate);
distZ += (velZ * intervalDate);
float distance = sqrtf(distX*distX+distY*distY+distZ*distZ);
[distanceLabel setText:[NSString stringWithFormat:@"%.2f",distance*0.0006213f]];
now_prev = [now retain];
}