我的问题如下: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