views:

431

answers:

1

fellas,

I am developing a application in which I need functionality to ask user for their location more than once, what happening is when user allowed it once to use his location and when he navigate to another section it's not asking him to get his location, it's taking it from already cached location.

is that possible to ask user multiple time for his approval to get his location?

Any help appreciated.

Cheers, Amit

+4  A: 

You don't need to get permission multiple times.

To begin getting updates, you call:

[locationManager startUpdatingLocation];

Until you call [locationManager stopUpdatingLocation] you will get continuous location updates.

So, you need to implement the delegate method to say what happens when you get a new location. This delegate method could do something as simple as save the location to a class variable for later use.

The delegate function you need to implement is:

- (void)locationManager:(CLLocationManager *)manager 
  didUpdateToLocation:(CLLocation *)newLocation
  fromLocation:(CLLocation *)oldLocation {}

I should caution you here. It is battery intensive to constantly run the GPS (you'll kill the device in about 2.5 hours if you are requesting very accurate reading as fast as possible). So, what you should probably do is get a fix when the user turns the app on, and then call stopUpdatingLocation.

Then, in your app, have a "Locate Me" button, which would turn on the LocationManager, get a fix, and then turn off the LocationManager again. You might want to keep polling for a location until you get a good location.horizontalAccuracy

I suggest you implement an NSObject subclass, which implements the LocationManagerDelegate protocol. Then, this object would be shared across multiple view controllers. Here is a simple implementation of a central gpsController.

So, this would be gpsController.h:

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface gpsController : NSObject <CLLocationManagerDelegate> {
  CLLocationManager *locationManager;
  CLLocation *lastReading;
}


- (id)init;

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *lastReading;

@end

And then the following is gpsController.m:

#import "gpsController.h"

@implementation gpsController

@synthesize locationManager, lastReading;

- (id)init {

  if(self = [super init]) {

    [[self locationManager] startUpdatingLocation]; 

    self.lastReading = nil;

  }

  return self;

}


- (CLLocationManager *)locationManager {

  if (locationManager) return locationManager;

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

  return locationManager;   

}


- (void)locationManager:(CLLocationManager *)manager 
  didUpdateToLocation:(CLLocation *)newLocation
  fromLocation:(CLLocation *)oldLocation {

  self.lastReading = newLocation;

}
Andrew Johnson