Вопрос

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. These are required parameters for a web service as well but it seems they are returned as nil.

The interface conforms to CLLocationManagerDelegate protocol and I have implemented the methods for it. 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

- (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 -

- (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. 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]; from the didUpdateLocations method

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)

Другие советы

You need to do in viewDidLoad like this

- (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 с атрибуция
Не связан с StackOverflow
scroll top