我的問題如下: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]];
}
}
任何建議將受到歡迎,並在此先感謝!
感謝您澄清它。我不得不問,stopUpdatingLocation調用仍然需要嗎?到目前爲止數據已正確加載,Web服務返回我需要的數據。現在我的最後一個問題是填充位置表,但是我再次只用模擬器進行測試。我手頭沒有設備。 – user3498133
一旦您擁有一個位置並且不再需要更新,那麼請調用'stopUpdatingLocation' - 這有助於節省電池使用時間,因爲如果不需要,iOS可以關閉位置硬件。你也可以使用Xcode中的調試菜單來模擬一個位置。 – Paulw11
保羅,已經嘗試過,但似乎沒有地點返回我當前的位置。我會嘗試更準確的座標。 – user3498133