4

I am working on map application,

I am try to use [locationManager stopUpdatingLocation]; method to stop the Location services.

It seems that it works fine in iOS4.3 but in iOS5, it's not working.

Please any one suggest me How to stop location services in iOS5?

Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
Anki
  • 589
  • 2
  • 13
  • 22
  • release your location service object, locationServiceManager = nil; if ARC – P.J Dec 24 '12 at 06:00
  • make certain that "`locationManager`" is not nil when you call it's "`stopUpdatingLocation`" method... – Michael Dautermann Dec 24 '12 at 06:05
  • Thanks prateek for comment..i am not using ARC in my app.. But i write a code like this.. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if (locationManager) { mapview.showsUserLocation = NO; locationManager = nil; [locationManager stopUpdatingLocation]; } } ... Is it ok to stop location service? – Anki Dec 24 '12 at 06:06
  • write locationManager = nil; at last, i.e. after [locationManager stopUpdatingLocation];, hope it helps you.. – P.J Dec 24 '12 at 06:25

2 Answers2

13

Sometimes it happens that location manager does not stop even after stopUpdationLocations.

You need to release your location manager, for that set:

locationManager = nil;

This should be written after:

[locationManager stopUpdatingLocation];

Also, take care you don't call locationManager object, after setting it to nil, else your application will crash.

If it stills not stopping location services, then there is work around solution, just use BOOL variable if you don't want to use it in future.

halfer
  • 19,824
  • 17
  • 99
  • 186
P.J
  • 6,547
  • 9
  • 44
  • 74
  • 1
    Thanks prateek, But i will need to call start updating location method, Because of every 2 minute i need to take current location latitude and longitude. Because of the battery drain problem i used to stopUpdatingLocation method when get a new location. – Anki Dec 24 '12 at 06:36
  • You can reallocate it and then then it can be called – P.J Dec 24 '12 at 06:37
5

You may get help from below two links:

LINK-1:

http://iphonedevsdk.com/forum/iphone-sdk-development/2299-cllocationmanager-stopupdatinglocation-not-working.html

Refer to site_reg's answer in above link:

I was having the same problem, and it seems to be fixed by declaring a static BOOL in your .m file and checking it when you enter the didUpdateToLocation method.

@implementation MyFile
@synthesize MySynth;
static BOOL haveAlreadyReceivedCoordinates = NO;

Then in your didUpdateToLocation method:

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

    if(haveAlreadyReceivedCoordinates) {
        return;
    }
    haveAlreadyReceivedCoordinates = YES;
    ...
}

This won't technically make it stop receiving updates, but it will ignore any updates after the first one. If your app depends on getting just one location update to do its work, this should help.

EDIT-1:

Also once this is done, you can add locationManager.delegate = nil; after [locationManager stopUpdatingLocation]; line.

LINK-2:

why self.locationManager stopUpdatingLocation doesn't stop location update

Refer to jnic's answer in above link:

The opposite of startMonitoringSignificantLocationChanges is not stopUpdatingLocation, it is stopMonitoringSignificantLocationChanges.

You probably want to replace startMonitoringSignificantLocationChanges with startUpdatingLocation for the sake of more regular updates, unless you have a specific reason for monitoring only for significant location changes.

Check out the CLLocation documentation for further detail.

I have added the answers from the link just to make sure that answer is useful even if the provided links goes down in future.

Community
  • 1
  • 1
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216