2014-05-06 62 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