2012-05-15 82 views
0

我正試圖計算從使用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]; 
} 

回答

1

至於爲什麼你的高度可能爲零,請see this answer to a similar question

這可能只是你的NSLog語句的問題,但無論是出發點和落腳點與NSLog聲明,說

NSLog(@"\nStarting point=%@", 

你似乎已經預定了3秒計時器的方式打印出來不是iOS要你使用CLLocationManager的方式。首選的方法是告訴CLLocationManager您的位置標準是什麼,然後纔開始更新。您實際上並不需要一直告訴它每3秒開始更新一次。你只需做一次,然後,如果你決定,你不需要任何更多的更新,然後調用

[locationManager stopUpdatingLocation]; 

如果OS沒有新的位置信息,它可能沒有任何意義不斷地問。它會通過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]; 

位置管理往往會救你多個結果,與它在錘鍊你的位置提高了精度。如果您不斷重新啓動它,我想知道是否會導致問題。

+0

我應該在viewDidload中使用上面的代碼。 –

+0

yes,@vikash,'viewDidLoad'可能是一個適當的地方來初始化'CLLocationManager'並開始更新。取決於您的應用程序的設置方式,最好將代碼放入'viewDidAppear:'中。這樣,無論用戶何時返回需要此代碼的視圖,該位置都會再次開始更新。你可能會選擇在'viewDidDisappear:'中調用'[locationManager stopUpdatingLocation]'。但是,這取決於您的應用是否有多個視圖,用戶需要花費多長時間才能看到每個視圖,以及您想要的效率。 – Nate

+0

只是爲了澄清,你可能''初始化'viewDidLoad'中的位置管理器,然後在'viewDidAppear:'和'viewDidDisappear:'實現中調用start/stop更新位置。 – Nate