Ramprasad
Ramprasad

Reputation: 8071

Fetch GPS location for every 5 minutes only

In my Location Application implemented didUpdateToLocation method. This method called every second and provides location data. But I dont need to fetch location for every second, I need to fire this method every 5 minutes only. Is it possible to do this?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //initialize location lisetener on Application startup
    self.myLocationManager = [[CLLocationManager alloc]init];
    self.myLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.myLocationManager.delegate = self;
    [self.myLocationManager startUpdatingLocation];


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[INNOViewController alloc] initWithNibName:@"INNOViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[INNOViewController alloc] initWithNibName:@"INNOViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}


-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    /*
    if(self.executingInBackground)
    {
        NSLog(@"Aplication running in background");
    }
    else
    {
        NSLog(@"Aplication NOT running in background");
    }
     */


    //NSLog(@"new location->%@ and old location -> %@",newLocation,oldLocation);

    NSString *urlAsString = @"http://www.apple.com";

    NSURL *url=[NSURL URLWithString:urlAsString];

    NSURLRequest *urlRequest=[NSURLRequest requestWithURL:url];

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];


    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

        if ([data length] > 0 && error == nil) {
            NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Downloaded html -> %@",html);
            //NSLog(@"Downloaded successfully");
        }
        else if([data length] == 0 && error == nil)
        {
            NSLog(@"Nothing downloaded");
        }
        else
        {
            NSLog(@"Error occured -> %@",error);
        }

    }];
}

Upvotes: 0

Views: 1155

Answers (4)

Midhun MP
Midhun MP

Reputation: 107231

Instead of:

[self.myLocationManager startUpdatingLocation];

use:

[self.myLocationManager startMonitoringSignificantLocationChanges];

If you use: startUpdatingLocation it'll call the delegate method in each second. When you use startMonitoringSignificantLocationChanges it'll call the delegate method when a significant change in location occurs or after 5 minute intervals.


startMonitoringSignificantLocationChanges

Starts the generation of updates based on significant location changes. - (void)startMonitoringSignificantLocationChanges

Discussion

This method initiates the delivery of location events asynchronously, returning shortly after you call it. Location events are delivered to your delegate’s locationManager:didUpdateLocations: method. The first event to be delivered is usually the most recently cached location event (if any) but may be a newer event in some circumstances. Obtaining a current location fix may take several additional seconds, so be sure to check the timestamps on the location events in your delegate method.

After returning a current location fix, the receiver generates 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. It does not rely on the value in the distanceFilter property to generate events. Calling this method several times in succession does not automatically result in new events being generated. Calling stopMonitoringSignificantLocationChanges in between, however, does cause a new initial event to be sent the next time you call this method.

If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrives. In such a case, the options dictionary passed to the locationManager:didUpdateLocations: method of your application delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your application was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.

In addition to your delegate object implementing the locationManager:didUpdateLocations: method, it should also implement the locationManager:didFailWithError: method to respond to potential errors.

Note: Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

Declared In CLLocationManager.h

Reference CLLocationManager

Upvotes: 1

Mindaugas
Mindaugas

Reputation: 1735

You could use startMonitoringSignificantLocationChanges instead of startUpdatingLocation. You would be updated only when user moves around 500 meters from last position

Upvotes: 1

borrrden
borrrden

Reputation: 33423

It is called that quickly because you asked for kCLLocationAccuracyBest. Back off a bit. It's not based on time, it's based on delta distance. At that accuracy even a small change in distance will trigger an update in an area with good reception. Use a different value.

Again, these methods are not meant to be used based on time. They are meant to be used based on delta distance.

Upvotes: 3

varun thomas
varun thomas

Reputation: 357

Reduce the accuraccy and the distance filter,this will reduce the frequency in which the method is called
If you want it to be called after 5 minutes then you can forcefully call the methods stopupdating and startupdating every five minutes

Upvotes: 2

Related Questions