Should app location tracking start to get ANY last known location from CLLocationManager?

An application is being developed that should receive the latest location from the CLLocationManager (without any regular tracking). No matter how old, accurate. I don’t need and you want to start tracking - I just need to take the last place from the cache and it. IMHO, CLLocationManager is a common component in iOS, and if some application uses location tracking, another application should be able to use the last location from CLLocationManager.location. Just select / init CLLocationManager and capture its location. However, it is not. I tested on iPhone4 - I started working with Google maps, saw my current location, and then went to my application, but after the [[CLLocationManager alloc] init] property was zero.

UPDATE: tried [locationManager startUpdatingLocation]; and [locationManager stopUpdatingLocation]; but the result is the same. I think the only solution is to start regular tracking?

UPDATE2: Strange, but no warning with “Application wants to use location services” after alloc / init CLLocationManager. Here is my code snippet:

CLLocationManager *locationManager = [[CLLocationManager alloc] init];

[locationManager startUpdatingLocation];
[locationManager stopUpdatingLocation];
NSLog(@"%@", locationManager.location); //prints nil
+5
source share
2 answers

First you need to check if your locationManagerlocation has a valid static location.

If so, you are done.

If not, you should startUpdatingLocation, and then, in the callback didUpdateToLocation:fromLocation:, stopUpdatingLocationafter receiving the location.

My experience says that the best way to get only one location.

UPDATE UPDATE AUTHORS:

stopUpdatingLocation startUpdatingLocation. startUpdatingLocation , , , .

+2

CLLocationManager - CLLocationManagerDelegate.

-[CLLocationManager startUpdatingLocation] . runloop, , .

:

@interface MyClass : NSObject <CLLocationManagerDelegate> {
    CLLocationManager *manager;
    CLLocation *lastLocation;
}

@end

@implementation

- (id)init {
    self = [super init];
    if (self) {
        manager = [[CLLocationManager alloc] init];
        manager.delegate = self;
        [manager startUpdatingLocation];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
{   
    lastLocation = newLocation;
    [manager stopUpdatingLocation];
}

// in your real implementation be sure to handle the error cases as well.
@end
+1

All Articles