我正試圖計算從使用Core Location框架開始的距離,但是當我將應用程序放在iPhone設備上時,數據不正確。距離持續波動並顯示隨機數據。請幫助我。此外,高度顯示爲零。在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];
}
我應該在viewDidload中使用上面的代碼。 –
yes,@vikash,'viewDidLoad'可能是一個適當的地方來初始化'CLLocationManager'並開始更新。取決於您的應用程序的設置方式,最好將代碼放入'viewDidAppear:'中。這樣,無論用戶何時返回需要此代碼的視圖,該位置都會再次開始更新。你可能會選擇在'viewDidDisappear:'中調用'[locationManager stopUpdatingLocation]'。但是,這取決於您的應用是否有多個視圖,用戶需要花費多長時間才能看到每個視圖,以及您想要的效率。 – Nate
只是爲了澄清,你可能''初始化'viewDidLoad'中的位置管理器,然後在'viewDidAppear:'和'viewDidDisappear:'實現中調用start/stop更新位置。 – Nate