我开始搞乱 CLLocationManager 并且我正在尝试使用下面的回调方法。我出去在我的 iPhone 上测试它,在最初的几次更新中,我似乎得到了乱码,就像突然之间我的距离是 39 米,即使我没有移动任何地方。或者有时它会从 5 开始,然后跳到 20 米,再次没有我移动任何地方。我出去走走,从最初的起点开始的更新看起来还不错,然后当我往回走时,我又回到了原来的 39 米起点。我想知道我在下面所做的是否正确。我还包括了我的 viewDidLoad 方法,我在其中初始化了我的 CLLocationManager 对象。
有没有办法确保我的第一个值是准确的?提前致谢。
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (startingPoint == nil) {
NSLog(@"NIL starting point");
self.startingPoint = newLocation;
}
CLLocationDistance distance = [newLocation distanceFromLocation:startingPoint];
NSString *distanceString = [[NSString alloc] initWithFormat:@"%g m", distance];
distanceLabel.text = distanceString;
[distanceString release];
numberOfUpdates++;
NSString *countString = [[NSString alloc] initWithFormat:@"%d", numberOfUpdates];
countLabel.text = countString;
[countString release];
}