evenodd
evenodd

Reputation: 2126

Speeding up Core Location

I am having a problem where it will sometimes take me 15 seconds or so to find my location. On the contrary, Google Maps will find my location within a few seconds, nearly every time. I am wondering if there is anyway I can speed up Core Location and CLLocationManager so that it finds the user location faster. Here is my setup

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.activityType = CLActivityTypeFitness;
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager startUpdatingLocation];
[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(checkAccuracy:) userInfo:nil repeats:YES];

For the CLLocationManagerDelegate I have:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation* location = [locations lastObject];
    NSDate* eventDate = location.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0) {
        // If the event is recent, do something with it.
        NSLog(@"latitude %+.6f, longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);

    [[NSNotificationCenter defaultCenter] postNotificationName:kLocationChanged object:nil];

}    

}

The notification at the end changes the map view center. Any idea on this? am I missing something?

Upvotes: 1

Views: 178

Answers (2)

Shibin S
Shibin S

Reputation: 149

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

It consumes more battery, but you will get more location updates. Be careful to filter signals of low accuracy and also to check timestamp of the GPS signal to ignore old updates.

Upvotes: 0

Andrea
Andrea

Reputation: 26383

The problem is the accuracy. You are not getting values until the location accuracy is near 10 meters.I don't know about you spec., but personally I use a wider accuracy and when I get the first location I change the required accuracy on the location manager.
The documentation says that:

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.

but in my experience is not like that, if I set for good accuracy it takes more time to have the first location, probably because it needs to "power up" more resources (is just a guess).

Upvotes: 1

Related Questions