8

So now I'm at least getting callbacks with the following code...

- (void)viewDidLoad {

[super viewDidLoad];
mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
//mapView.showsUserLocation=TRUE;
mapView.delegate=self;
[self.view insertSubview:mapView atIndex:0];

NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO");
    CLLocationManager *newLocationManager = [[CLLocationManager alloc] init];
    [newLocationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [newLocationManager setDistanceFilter:kCLDistanceFilterNone];
    [self setLocationManager:newLocationManager];


[[self locationManager] setDelegate:self];
[[self locationManager] startUpdatingLocation];
NSLog(@"Started updating Location");

}


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

NSLog(@"Did update to location");
mStoreLocationButton.hidden=FALSE;
location=newLocation.coordinate;

MKCoordinateRegion region;
region.center=location;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;


[mapView setRegion:region animated:TRUE];


}

I can set breakpoints in the second method and NSLog is reporting continual location updates, but for some reason the zoom with span isn't working. Any idea why? It's got my coordinates and everything. Sort of scratching my head on this one.

Coltrane
  • 165
  • 1
  • 2
  • 12

7 Answers7

16

Assign the CLLocationManager to a (strong) property on your class. (I assume you're using ARC BTW.) Right now the CLLocationManager doesn't live past the end of the viewDidLoad method, so it won't get to call your delegate method either.

Johan Kool
  • 15,637
  • 8
  • 64
  • 81
  • Didn't seem to make a difference. Again, I'm testing on the simulator. Will test of a device Monday. – Coltrane Jul 28 '12 at 17:41
  • Sorry, my mistake. You are exactly right. I'm now getting callbacks "did update to location". My problem now, is that the zoom using span isn't working. Any further ideas on that? – Coltrane Jul 30 '12 at 21:18
  • +1, Worked for me. This was the only issue in my case. Cheers. – viral Oct 06 '14 at 11:51
14

Make sure that you've added <CLLocationManagerDelegate> in the @interface file.

Edit:

If the delegate is set properly, make sure you're using your locationManager property:

In the .h file:

@property (nonatomic, strong) CLLocationManager *locationManager;

In viewDidLoad:

self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];
nevan king
  • 112,709
  • 45
  • 203
  • 241
5

I think, You can make this work by two ways:

  1. Using CLLocation framework

Check that, you have adopted the ViEWController with CLLocationManagerDelegate methods

#import<MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate, 
                                              MKMapViewDelegate>
{
    CLLocationCoordinate2D location;
    MKMapView *mapView;
}
@end

In ViewController.m:

@implementation GSViewController
- (void)viewDidLoad {

    [super viewDidLoad];
    mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
    mapView.showsUserLocation=TRUE;
    mapView.delegate=self;
    [self.view insertSubview:mapView atIndex:0];


    CLLocationManager *locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;

    [locationManager startUpdatingLocation];

}

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


    NSLog(@"new location: %@", newLocation);
    location=newLocation.coordinate;

    MKCoordinateRegion region;
    region.center=location;
    MKCoordinateSpan span;
    span.latitudeDelta=0.01;
    span.longitudeDelta=0.01;
    region.span=span;

    [mapView setRegion:region animated:TRUE];

}


-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"error: %@", error.description);
}
@end

2.Using the same MKMapKit framework You can do this by using the MKMapViewDelegate method named didUpdateUserLocation: Here you don't need the CLLocaionManager, This will be done by: In ViewController.h:

#import <MapKit/MapKit.h>
@interface ViewController : UIViewController < MKMapViewDelegate>
{
    CLLocationCoordinate2D location;
    MKMapView *mapView;
}
@end

and In ViewController.m file:

@implementation GSViewController
- (void)viewDidLoad {

    [super viewDidLoad];
    mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
    mapView.showsUserLocation=TRUE;
    mapView.delegate=self;
    [self.view insertSubview:mapView atIndex:0];

}

-(void)mapView:(MKMapView *)mapV didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"map new location: %f %f", userLocation.coordinate.latitude, userLocation.coordinate.longitude);
    location=userLocation.coordinate;

    MKCoordinateRegion region;
    region.center=location;
    MKCoordinateSpan span;
    span.latitudeDelta=0.1;
    span.longitudeDelta=0.1;
    region.span=span;

    [mapV setRegion:region animated:TRUE];
}
@end
Shanmugaraja G
  • 2,778
  • 4
  • 31
  • 47
  • Using method 1, I'm getting callbacks, but the zoom (span) isn't zooming to location. I revised my code. Maybe take a look and see if something is wrong? – Coltrane Jul 30 '12 at 21:17
1

Well, first of all, you can never be sure that the location manager is able to update the location in the first place. There could be an error during update or you don't have access to the user's location.

Implement this CLLocationManager delegate method and verify the error.

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

"Implementation of this method is optional. You should implement this method, however."

0x8b4df00d
  • 53
  • 1
  • 8
  • 1
    Not reporting any errors. I am attempting this test on the iOS simulator. Could that have anything to do with it? – Coltrane Jul 28 '12 at 04:43
1

If you're running this in the simulator only you might need to prompt it to change coordinates. In Xcode there is a bar above the debug output pane with the typical Location Services arrow. Next to that is a drop down list of locations. Once your app is running, switch the location it is simulating and see if that change triggers your code. Then test it on a real device.

Craig
  • 8,093
  • 8
  • 42
  • 74
  • That's pretty clever. Unfortunately it didn't work, but I like the idea. I'll just need to test on a real device. – Coltrane Jul 28 '12 at 17:44
0

In my case, it didn't enter on didChangeAuthorization. I was trying on a real device.

I deleted the application from the phone and install it again. And it works!

Hope this helps someone :)

Dx_
  • 1,566
  • 1
  • 16
  • 17
0

For Swift 3

The method has changed to:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

from:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!)

Notice the '_' and the cast of 'locations' to [CLLocation] rather than [AnyObject]!

If you use the old method it will never be called and you won't receive a warning that it has changed.

Chris
  • 955
  • 15
  • 20