4

I am working on walking track type app but facing problem that how location would upadte when app is in background . I am drawing a path on map as location update and a timer also . Then please suggest me how i handle it. here is my code

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{  
    if(!newLocation) return;

    if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
        (oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
    {        
      // to  draw path as new location find
        jogPoint = [[JogPoint alloc] initWithCenterCoordinate:newLocation.coordinate];

        [jogPoints addObject:jogPoint];


         if([jogPoints count] >= 5) {

            [self.mapView addOverlay:jogPoint];

            CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];


            CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
            // returns distance in meters
          distnc+=[loc1 distanceFromLocation:loc2] ;
            distanceLabel.text=[NSString stringWithFormat:@"%lf",distnc];

       //  jogInfo.distance += ([loc1 distanceFromLocation:loc2]) * 0.000621371192;

           // jogInfo.eclapsedTime = (CFAbsoluteTimeGetCurrent() - startTime) * 1000 * 60;



        }
    }
 }
iPatel
  • 46,010
  • 16
  • 115
  • 137
Arpi
  • 397
  • 2
  • 5
  • 14

2 Answers2

4

If your Application is in background mode also location is tracking/update as like if your app. active so don't worry about it :)

Just Make sure your create CLLocationManager such like

self.currentLocation = [[CLLocationManager alloc]init];
self.currentLocation.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.currentLocation.delegate = self;
[self.currentLocation startUpdatingLocation];

EDITED :

If here set distanceFilter then your method call at specific meter which value set in distanceFilter otherwise it update whenever your device is move

And This is very important to set/add Require background mode in YourProjectName-Info.plist file Value is App Registers for location update

Such like

enter image description here

iPatel
  • 46,010
  • 16
  • 115
  • 137
  • Would it update timer and there is no need to add required background mode in info.plist file. – Arpi Sep 12 '13 at 06:15
  • didUpdateToLocation automatically call on your movement if distanceFilter is kCLDistanceFilterNone. – iPatel Sep 12 '13 at 06:17
  • you mean to say i don't need to do any additional code for it. – Arpi Sep 12 '13 at 06:27
  • @Arpi - Yes i', exactly :) – iPatel Sep 12 '13 at 06:35
  • 1
    Make sure that your distanceFilter is not set and also does not stop `stopUpdatingLocation` so.. it will continue call `didUpdateToLocation ` method – iPatel Sep 12 '13 at 06:42
  • @Arpi - now check my full correct answer :) your `didUpdateToLocation` method call even your application is in background mode :) – iPatel Sep 12 '13 at 11:49
  • K Let me Check @iPatel – Arpi Sep 12 '13 at 11:56
  • I have done with it. But I am not getting further process/code as discussed in following link http://stackoverflow.com/questions/10235203/getting-user-location-every-n-minutes-after-app-goes-to-background – Arpi Sep 12 '13 at 12:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37260/discussion-between-ipatel-and-arpi) – iPatel Sep 12 '13 at 12:18
0

You can use the significant-change location service to receive location events.

This service offers a significant power savings and provides accuracy that is good enough for most apps. It uses the device’s cellular radio to determine the user’s location and report changes in that location, allowing the system to manage power usage much more aggressively than it could otherwise. This service is also capable of waking up an app that is currently suspended or not running in order to deliver new location data.

To use the significant-change location service:

Create an instance of the CLLocationManager class, assign a delegate to it, and call the startMonitoringSignificantLocationChanges method . As location data becomes available, the location manager notifies its assigned delegate object. If a location update has already been delivered, you can also get the most recent location data directly from the CLLocationManager object without waiting for a new event to be delivered.

thatzprem
  • 4,697
  • 1
  • 33
  • 41