bruno
bruno

Reputation: 2169

Update location from time to time

I´m getting updates of my location using the below code:

- (id)init {
    if (self = [super init]) 
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager startUpdatingLocation];
    }

    return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"Location: %@", [locations lastObject]);
}

Everything works fine, the problem is that the updates are coming with 1 second of interval. Is there any way to make the intervals longer?

Upvotes: 0

Views: 242

Answers (3)

Aaron
Aaron

Reputation: 7145

Your problem is the distance filter setting. kCLLocationAccuracyBest gives you the best data but you'll get updates almost constantly. Now unless you're building a directions/routing application this probably isn't what you want because it's not efficient.

Set the distance filter like so:

_locationManager = [[CLLocationManager alloc] init];
_locationManager.distanceFilter = kCLLocationAccuracyHundredMeters; // One of several options...

Alternatively, as has been mentioned there is a significant change API that you can use to receive updates. These will occur less frequently by design, but you don't have much control over how often you receive the updates.

Call this method to start receiving those updates:

[_locationManager startMonitoringSignificantLocationChanges]

Its most common to do this while your app is backgrounded.

Check the docs for more info:

https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

Upvotes: 3

Anindya Sengupta
Anindya Sengupta

Reputation: 2579

Option 1

Use - (void)startMonitoringSignificantLocationChanges on your location manager which will update events only when a significant change in the user’s location is detected. For example, it might generate a new event when the device becomes associated with a different cell tower.

Option 2

You can try to reduce the accuracy of monitoring.

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];

Do not use kCLDistanceFilterNone as it will report every movement.

"All movements are reported"

and set accuracy value to something among:

kCLLocationAccuracyNearestTenMeters
kCLLocationAccuracyHundredMeters 
kCLLocationAccuracyKilometer 
kCLLocationAccuracyThreeKilometers

Upvotes: 1

sage444
sage444

Reputation: 5684

just use startMonitoringSignificantLocationChanges is should help also set distanceFilter

And of course refer to docs CLLocationManager Class Reference

Upvotes: 1

Related Questions