2014-05-06 59 views
0

我的問題如下:AFNetworking和CLLocation Manager iOS

使用位置服務時何時更新位置?當我打電話給startUpdatingLocation時,我希望已經返回了一個位置,所以我可以爲我的iOS項目檢索經度和緯度。這些也是Web服務的必需參數,但似乎它們返回爲零。

接口符合CLLocationManagerDelegate協議,我已經實現了它的方法。反正這裏是我的代碼:

- (void)viewDidLoad 
{ 
     super viewDidLoad]; 

// Uncomment the following line to preserve selection between presentations. 
// self.clearsSelectionOnViewWillAppear = NO; 

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
// self.navigationItem.rightBarButtonItem = self.editButtonItem; 
if([self.parentViewController isKindOfClass:[BTMainViewController class]]) 
{ 
    BTMainViewController *parent = (BTMainViewController *)self.parentViewController; 
    self.sessionKey = parent.session; 
    NSLog(@"URL is %@ ", self.sessionKey); 
} 
locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

// also set the URL 
self.serviceURL = [apiURL stringByAppendingString:@"/get_employee_closestlocations"]; 

// set tableview delegate and data source 
self.tableView.delegate = self; 
self.tableView.dataSource = self; 

// adjust for EdgeInset with navigation bar. 
self.tableView.contentInset = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f); 


// fetch the locations here 
[locationManager startUpdatingLocation]; 
[self fetchLocations]; 

} 

didUpdateToLocation實施

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 

CLLocation *currentLocation = [locationManager location]; 
[locationManager stopUpdatingLocation]; 

if(currentLocation != nil) 
{ 
    [self setLongitude:[NSNumber numberWithDouble: currentLocation.coordinate.longitude]]; 
    [self setLatitude:[NSNumber numberWithDouble: currentLocation.coordinate.latitude]]; 
} 
} 

任何建議將受到歡迎,並在此先感謝!

回答

0

您正在使用的委託方法已被棄用。您應該使用locationManager:didUpdateLocations:,然後從數組結束訪問位置更新 -

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *currentLocation = (CLLocation *)[locations lastObject]; 
    ... 
} 

這可能需要一些時間來獲得位置鎖定,尤其是在你指定kCLLocationAccuracyBest - iOS設備可能需要啓動的GPS接收器,如果它最近沒有被使用過,那麼GPS需要獲得修復 - 如果設備在裏面或GPS接收不好,這可能會進一步延遲一個位置的採集。您可以通過重新啓動設備,啓動地圖應用程序並點擊位置「箭頭」並等待藍色位置圓圈摺疊到藍色&白色標記,來獲得修復時間。

我建議你從didUpdateLocations方法

而且調用您[self fetchLocations];Core Location documentation美國 -

當要求高精度的位置數據,通過位置服務交付的初始事件 可能沒有您要求的準確性 。位置服務儘可能快地發送初始事件 。然後,它會繼續根據您請求的準確性確定該位置,並根據需要提供其他事件, (該數據可用時)。

因此,當您訪問該位置時,可能會有不準確的風險。你可以看一下CLLocationhorizontalAccuracy財產,並決定是否要接受這個位置,或者等待更準確的位置(在銘記,它可能無法到達,如果在設備內部或信號不好)

+0

感謝您澄清它。我不得不問,stopUpdatingLocation調用仍然需要嗎?到目前爲止數據已正確加載,Web服務返回我需要的數據。現在我的最後一個問題是填充位置表,但是我再次只用模擬器進行測試。我手頭沒有設備。 – user3498133

+0

一旦您擁有一個位置並且不再需要更新,那麼請調用'stopUpdatingLocation' - 這有助於節省電池使用時間,因爲如果不需要,iOS可以關閉位置硬件。你也可以使用Xcode中的調試菜單來模擬一個位置。 – Paulw11

+0

保羅,已經嘗試過,但似乎沒有地點返回我當前的位置。我會嘗試更準確的座標。 – user3498133

0

你需要這樣下面的委託方法將自動調用後viewDidLoad中做這樣的

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 


    mapView.delegate = self; 
    mapView.showsUserLocation = YES; // Enable it when we want to track user's current location. 

} 

- (void)mapView:(MKMapView *)mapView 
       didUpdateUserLocation: 
       (MKUserLocation *)userLocation 
{ 
    self.mapView.centerCoordinate = userLocation.location.coordinate; 
} 
+0

OP沒有提到'MKMapView' – Paulw11

+0

ohh!哎呀,它被我誤解了。 –

+0

沒問題,我想要的就是位置。它不是一個真正的使用應用程序的地圖......不過:)在另一個說明中,Paul,似乎該服務沒有返回任何靠近iOS Simulator所接受座標的附近位置。將不得不在設備上進行測試。 – user3498133