我正在尝试计算从使用Core Location框架开始的距离,但是当我将应用程序放在iPhone设备上时,数据不正确。距离不断波动并显示随机数据。请帮助我。此外,海拔高度显示为零。
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//Altitude
if(startingPoint==nil)
self.startingPoint=newLocation;
NSString *currentAltitude = [[NSString alloc] initWithFormat:@"%g",
newLocation.altitude];
heightMesurement.text=currentAltitude;
[currentAltitude release];
//Distance
if(startingPoint==nil)
self.startingPoint=newLocation;
//if(newLocation.horizontalAccuracy <= 100.0f) {
// [locationManager stopUpdatingLocation];
//}
//test start.......................................................
//startlocation
NSString *sp = [[NSString alloc]
initWithFormat:@"%f",startingPoint];
NSLog(@"\nStarting point=%@",startingPoint);
ssp.text=sp;
//endlocation
NSString *ep = [[NSString alloc]
initWithFormat:@"%f",newLocation];
NSLog(@"\nStarting point=%@",newLocation);
eep.text=ep;
//test end............................................................
CLLocationDistance mydistance=[newLocation distanceFromLocation:startingPoint];
NSString *tripString = [[NSString alloc]
initWithFormat:@"%f",mydistance];
distLabel.text=tripString;
[tripString release];
//test........................
[sp release];
[ep release];
}//Location Manager ends..
//Time interval of 3 sec....
-(void)locationUpdate:(NSTimer*)timer{
if(timer != nil) [timer invalidate];
[self.locationManager startUpdatingLocation];
}
答案 0 :(得分:1)
至于为什么你的海拔高度可能为零,请see this answer to a similar question。
这可能只是您NSLog
语句的问题,但起点和终点都打印出NSLog
语句,表示
NSLog(@"\nStarting point=%@",
您似乎计划安排3秒计时器的方式并不是iOS希望您使用CLLocationManager
的方式。首选方法是告诉CLLocationManager
您的位置标准是什么,然后开始更新。实际上你并不需要告诉它每3秒开始更新一次。您可以只执行一次,然后如果您决定不再需要更新,请致电
[locationManager stopUpdatingLocation];
如果操作系统没有新的位置信息,那么继续询问可能没有意义。它会通过locationManager:didUpdateToLocation:fromLocation
告诉您何时有新的位置信息。所以,我建议更像这样开始这个过程:
CLLocationManager* locationManager = [[CLLocationManager alloc] init];
if ([locationManager locationServicesEnabled]) {
Reachability* netStatus = [Reachability sharedReachability];
if (([netStatus internetConnectionStatus] != NotReachable) || ([netStatus localWiFiConnectionStatus] != NotReachable)) {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = 100.0; // 100 m, or whatever you want
[locationManager startUpdatingLocation];
} else {
// TODO: error handling
}
}
self.locMgr = locationManager;
[locationManager release];
位置管理员通常会为您提供多种结果,并且随着您的位置进行操作,准确度会越来越高。如果你不断重新启动它,我想知道这是否会导致问题。