1

i am trying to get user's location in app's terminated state. i am doing this by startMonitoringSignificantLocationChanges but it's giving location after 3km or after 5 min. so can't create a route properly. please guide me how to do this.

 if (_anotherLocationManager)
    [_anotherLocationManager stopMonitoringSignificantLocationChanges];

self.anotherLocationManager = [[CLLocationManager alloc]init];
_anotherLocationManager.delegate = self;
_anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

if(IS_OS_8_OR_LATER) {
    [_anotherLocationManager requestAlwaysAuthorization];
}
[_anotherLocationManager startMonitoringSignificantLocationChanges];
Mohit Tomar
  • 5,173
  • 2
  • 33
  • 40

3 Answers3

0

I solved my query myself...

Create a locationManager object and alloc it like this

 self.locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // setting the accuracy
[self.locationManager requestAlwaysAuthorization];

if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
    [self.locationManager setAllowsBackgroundLocationUpdates: YES];
}

self.locationManager.distanceFilter = 20.0;

[self.locationManager startMonitoringSignificantLocationChanges];

self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;

[self.locationManager startUpdatingLocation];

self.locationManager.pausesLocationUpdatesAutomatically = YES;

self.locationManager.delegate = self;

Now set location manager delegate method.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
{
    if (newLocation.speed>7){
      // here you got location, i settled speed 7 for my convenience.  
    }

    if (newLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy) {
        //Desired location Found
        [self.locationManager stopUpdatingLocation ];
    }
}

You must have to write this stopUpdatingLocation, else your battery consumption will increase so high.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mohit Tomar
  • 5,173
  • 2
  • 33
  • 40
  • incorrect, the documentations states different: "After returning a current location fix, the receiver generates update events only when a significant change in the user’s location is detected. It does not rely on the value in the distanceFilter property to generate events." – DoruChidean Jan 23 '18 at 12:29
0

Use the Geofencing to achieve the same.

  • Create a Geofence of 200mtr radius.
  • By default, notifyOnExit would be true
  • Implement the delegate didExitRegion of LocationManager
  • For termination state, the app would be lunched with UIApplication.LaunchOptionsKey.location in didFinishLaunchingWithOptions.
  • Create an instance of Location Manager object on location launch key, you obtain the region at which the location exited and keep creating the 200mtr fence on every exit.
Mahesh S
  • 309
  • 1
  • 11
-1

You're not going to get 200 meter granularity or continuous tracking with significant location monitoring.

Have you seen these notes in the docs:

The significant-change location service delivers updates only when there has been a significant change in the device’s location, such as 500 meters or more.

If GPS-level accuracy isn’t critical for your app and you don’t need continuous tracking, you can use the significant-change location service.

Mark Krenek
  • 4,889
  • 3
  • 25
  • 17