AFNetworking和CLLocation Manager iOS

时间:2014-05-06 08:54:50

标签: ios afnetworking locationmanager

我的问题如下:

使用位置服务时,位置何时更新?当我调用startUpdatingLocation时,我希望已经返回了一个位置,这样我就可以检索iOS项目的纬度和经度。这些也是Web服务的必需参数,但似乎它们以nil返回。

接口符合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]];
}
}

欢迎任何建议,并提前致谢!

2 个答案:

答案 0 :(得分:0)

不推荐使用您正在使用的委托方法。您应该使用locationManager:didUpdateLocations:,然后从阵列末尾访问位置更新 -

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

可能需要一些时间来修复位置,特别是当您指定了kCLLocationAccuracyBest时 - 如果最近没有使用过,那么iOS可能需要启动GPS接收器然后GPS需要获得修复 - 如果设备在内部或GPS接收不良,这可能会进一步延迟获取位置。您可以通过重新启动设备,启动地图应用程序并点击位置" arrow"来了解获取修复的时间。并等到蓝色位置圆圈向下折叠到蓝色&白色标记。

我建议您从[self fetchLocations];方法

调用didUpdateLocations

此外,Core Location documentation州 -

  

请求高精度位置数据时,初始事件   由位置服务提供可能没有您的准确性   请求。定位服务可以快速提供初始事件   尽可能。然后它继续确定位置   您要求的准确性,并根据需要提供其他活动,   当这些数据可用时。

因此,当您访问该位置时存在风险,可能不是特别准确。您可以查看horizontalAccuracy的{​​{1}}属性,并决定是否要接受此位置或等待更准确的位置(请注意,如果设备位于内部或具有此位置,则可能无法到达接待不好)

答案 1 :(得分: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;
}
相关问题