0

I have the following code:

- (void) viewWillAppear {
    [self startSignificantChangeUpdates];
}



- (void)startSignificantChangeUpdates
{


    if ([CLLocationManager locationServicesEnabled])
    {
        NSLog(@"start significant changes");

        if (nil == locationManager)
            locationManager = [[CLLocationManager alloc] init];

        locationManager.delegate = self;
        [locationManager startMonitoringSignificantLocationChanges];
    }
}


problem is that the location manage is not  calling its delegate function 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

What can I do?

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165
  • It may not solve your problem, but I would recommend using `didUpdateLocations:` instead of `didUpdateToLocation:fromLocation` (see http://stackoverflow.com/questions/12602463/didupdatelocations-instead-of-didupdatetolocation) if only because Apple has deprecated it. – Jake Spencer Jul 30 '13 at 20:38

3 Answers3

1

In your .h file:

#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>

In your .m file:

@implementation ViewController {
    // This is a private variable, used within this file only
    CLLocationManager *locationManager;
}

-(void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
}

-(void)viewWillAppear {
    // If the delegate is still not being set, try putting this code into viewDidAppear
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

-(void)locationManger:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError: %@", error);
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    NSLog(@"didUpdateToLocation: %@", [locations lastObject]);
    CLLocation *currentLocation = [locations lastObject];
    [locationManager stopUpdatingLocation];
}

For more information about getting the user's location, look at this guide, which I used to implement location into an app of mine: http://www.appcoda.com/how-to-get-current-location-iphone-user/

David
  • 35
  • 4
0

How are you testing this? If you are running this in the simulator, you can go to Debug->Location->Freeway Drive. You can put a break point in locationManager:didUpdateToLocation method or even put a log statement to log the current location coordinates. You should see it work.

Hetal Vora
  • 3,341
  • 2
  • 28
  • 53
  • It doesn't get your current location right away? Why does it wait until you change your location? Can I change that? – Chris Hansen Jul 30 '13 at 15:54
  • It does get your current location. It may take a few seconds to get it though. After the current location, it would need significant change in the location to happen to report back (e.g. cell tower change). Hence, with a Freeway ride, the simulator helps you detect significant updates. – Hetal Vora Jul 30 '13 at 16:43
  • problem is for some reason its still not getting the user's current location even after a few seconds... – Chris Hansen Jul 30 '13 at 17:16
0

Please replace this line

[locationManager startMonitoringSignificantLocationChanges]; 

with this code

[locationController.locationManager startUpdatingLocation];

and then check

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
}
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Banker Mittal
  • 1,918
  • 14
  • 26