2013-05-20 52 views
2

我試圖爲LBS應用程序進行實驗時遇到了問題。當在模擬器中測試時,它似乎工作。然後,我在iPhone上試了一下。它沒有按預期工作。CLLocationManager沒有正確更新數據

當我啓動應用程序時,它會顯示所有信息,例如我的位置緯度,經度,距離等等。但是,當我走開時​​,這些信息沒有改變。無論是運行應用程序還是我的iPhone自動關閉(然後再次打開應用程序),這些數據仍然保持不變。

雖然我關閉應用程序並重新啓動它,但它可以顯示更新信息。

這裏是我的代碼

我成立了CLLocationManager在viewDidLoad中

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //setup locationManager 
    locationManager =[[CLLocationManager alloc]init]; 
    locationManager.desiredAccuracy=kCLLocationAccuracyBest; 
    locationManager.distanceFilter=kCLDistanceFilterNone; 
    [locationManager setDelegate:self]; 
    [locationManager startUpdatingLocation]; 

} 

這裏是CLLocationManager代表

碼 - (空)的LocationManager:(CLLocationManager *)經理didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

if (newLocation.horizontalAccuracy < 0) return; 

NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 

if (locationAge > 5.0) return; 


if (myLocation == nil || myLocation.horizontalAccuracy > newLocation.horizontalAccuracy) { 

    self.myLocation = newLocation; 
    // NSLog(@"mylocation=%@",self.myLocation); 
    [self.mainTableView reloadData]; 
    [self sortedDistArray];//this method is to calculate distance and put it in array. It work... 


    if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) { 
     [self stopLocationManager];  
    } 
} 

}

  • (無效)stopLocationManager { [的LocationManager stopUpdatingLocation]; locationManager.delegate = nil; }

現在,我有以下問題。

  1. 我的CLLocationManager設置有什麼問題?我應該在appDelegate下面(在didFinishLaunchingWithOptions方法下)讓它運行它的背景嗎?它會快速耗盡電池電量嗎?或者將它設置在ViewWillAppear而不是ViewDidLoad中?

  2. 另一方面,我把'拉下來刷新'到程序中。我認爲刷新它時可以更新更新的數據。但是,它不會起作用。這裏是我的刷新代碼(我把EGOTableViewPullRefresh用於刷新功能)。

    • (無效){reloadTableViewDataSource

      // should be calling your tableviews data source model to reload 
          // put here just for demo 
      
      _reloading = YES; 
      
      locationManager =[[CLLocationManager alloc]init]; 
      locationManager.desiredAccuracy=kCLLocationAccuracyBest; 
      locationManager.distanceFilter=kCLDistanceFilterNone; 
      [locationManager setDelegate:self]; 
      [locationManager startUpdatingLocation]; 
      
      [self sortedDistArray]; 
      [mainTableView reloadData]; 
      
      [self doneLoadingTableViewData]; 
      

      }

什麼是我的想法是CLLocationManager復位並開始更新位置時用戶刷新。但是,它沒有工作。我的錯是什麼?

對不起,我有這麼多問題。如果有人給我建議,我將不勝感激。

+0

檢查文檔怎麼我認爲這是對的位置兩次連續調用之間的時間延遲。 –

回答

0

您無法控制何時提供位置更新,您只能啓動CLLocationManager,操作系統會決定何時爲您的CLLocationManagerDelegate提供更新。

如果您希望它強制更新,您可以嘗試檢查CLLocationManager是否已經啓動,如果是,請停止並重新啓動。順便說一下,作爲一個附註,我建議你將CLLocationManager包裝在一個單例類中。如果你繼續啓動位置管理器,它會在很多情況下使用應用程序運行並耗盡電池(當你實際停止時,我看不到你的代碼)。根據需要啓動和停止CLLocationManager

0

我不清楚你到底想要什麼。如果您只想在打開應用程序時顯示位置,請將startupdatinglocation行移至viewDidAppear。在屏幕上顯示一個活動指示器,因爲它需要一些時間回到並更新。

如果您希望它不斷更新,請不要調用stopUpdatingLocation。只要您的位置更改以符合經理的設置,它就會觸發委託方法。

另一件事我注意到,你的意思是說

if (locationAge < 5.0) return; 

看來你比5.0大說在這種情況下,如果花費長於得到更新,你將永遠不會到其他迴應。

而不是重複設置您的經理,創建一個單身或保存在一個屬性。一旦啓動,您只需根據需要啓動/停止更新。

您可能會受益於一些我在這裏的技巧:

TTLocationHandler GitHub Repository

+0

由於筆記本電腦壞掉了T_T,我剛回到這裏。感謝您的慷慨建議。事實上,我的問題是在某些地下區域不能工作。 TTLocationHandler是否在地下工作?我只想讓用戶可以在地下觸發應用程序(即我不會嘗試通過地下區域的轉向導航)。 –

+0

還有一件事...我找到了關於將CLLocationManager包裝在Internet上的單例類中的方法。鏈接是http://jinru.wordpress.com/2010/08/15/singletons-in-objective-c-an-example-of-cllocationmanager/....希望它能幫助像我這樣的人 –