简体   繁体   English

AFNetworking和CLLocation Manager iOS

[英]AFNetworking and CLLocation Manager iOS

My question is as follows: 我的问题如下:

When is the location updated when using Location Services? 使用定位服务时,何时更新位置? When I called startUpdatingLocation I expected to already have a location returned so I can retrieve latitude and longitude for my iOS project. 当我调用startUpdatingLocation时,我希望已经有一个返回的位置,以便可以为我的iOS项目检索纬度和经度。 These are required parameters for a web service as well but it seems they are returned as nil. 这些也是Web服务的必需参数,但似乎它们返回为nil。

The interface conforms to CLLocationManagerDelegate protocol and I have implemented the methods for it. 该接口符合CLLocationManagerDelegate协议,我已经实现了该方法。 Anyway here is my code: 无论如何,这是我的代码:

- (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 implementation 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]];
}
}

Any suggestions would be welcome and thanks in advance! 任何建议将受到欢迎,并在此先感谢!

The delegate method you are using is deprecated. 您使用的委托方法已弃用。 You should use locationManager:didUpdateLocations: and then access the location update from the end of the array - 您应该使用locationManager:didUpdateLocations: ,然后从数组末尾访问位置更新-

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

It can take some time to get a location fix, particularly as you have specified kCLLocationAccuracyBest - iOS may need to start up the GPS receiver if it hasn't been used recently and then the GPS needs to obtain a fix - if the device is inside or has bad GPS reception this can further delay the acquisition of a location. 获取位置修复可能需要一些时间,尤其是您已指定kCLLocationAccuracyBest如果最近未使用过iOS,则iOS可能需要启动GPS接收器,然后GPS需要获取修复-如果设备位于内部或GPS接收不良,可能会进一步延迟位置的获取。 You can get an idea of the time to obtain a fix by restarting your device, starting the maps application and tapping the location "arrow" and waiting until the blue location circle collapses down to the blue & white marker. 您可以通过重新启动设备,启动地图应用程序并点按“箭头”位置,等到蓝色位置圆圈折叠成蓝色和白色标记,来获取修复的时间。

I would suggest that you invoke your [self fetchLocations]; 我建议您调用您的[self fetchLocations]; from the didUpdateLocations method didUpdateLocations方法

Also, the Core Location documentation states - 此外,“ 核心位置”文档还指出-

When requesting high-accuracy location data, the initial event delivered by the location service may not have the accuracy you requested. 当请求高精度位置数据时,位置服务传递的初始事件可能不具有您要求的准确性。 The location service delivers the initial event as quickly as possible. 定位服务会尽快传递初始事件。 It then continues to determine the location with the accuracy you requested and delivers additional events, as necessary, when that data is available. 然后,它将继续以您要求的精度确定位置,并在有可用数据时根据需要提供其他事件。

So, there is a risk that when you do access the location, it may not be particularly accurate. 因此,存在风险,当您确实访问该位置时,它可能不是特别准确。 You can look at the horizontalAccuracy property of the CLLocation and decide whether you want to accept this location or wait for a more accurate location (bearing in mind that it may not arrive if the device is inside or has poor reception) 您可以查看CLLocationhorizontalAccuracy属性,并确定是要接受该位置还是要等待更准确的位置(请注意,如果设备在内部或接收不良,则可能无法到达)

You need to do in viewDidLoad like this 您需要像这样在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.

}

after doing this the below delegate method will automatically called. 完成此操作后,将自动调用以下委托方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM