CLLocationManager startUpdatingLocation not working
Asked Answered
A

7

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.

Andel answered 28/7, 2012 at 2:23 Comment(3)
ur code seems ok.. problem is smwhere elseDisavowal
Are you having those troubles in the simulator ? Have you enabled the simulation of location ?Strickle
Yes, simulator troubles. I have enabled simulation location. Perhaps I need to test it on a device.Andel
W
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.

Wrightson answered 28/7, 2012 at 14:38 Comment(3)
Didn't seem to make a difference. Again, I'm testing on the simulator. Will test of a device Monday.Andel
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?Andel
+1, Worked for me. This was the only issue in my case. Cheers.Respectability
I
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];
Indusium answered 28/7, 2012 at 9:29 Comment(1)
That would only fix warnings, not much else.Wrightson
H
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
Hultin answered 28/7, 2012 at 6:13 Comment(1)
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?Andel
D
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."

Dinnerware answered 28/7, 2012 at 4:5 Comment(1)
Not reporting any errors. I am attempting this test on the iOS simulator. Could that have anything to do with it?Andel
G
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.

Geniagenial answered 28/7, 2012 at 14:18 Comment(1)
That's pretty clever. Unfortunately it didn't work, but I like the idea. I'll just need to test on a real device.Andel
Y
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 :)

Year answered 15/10, 2016 at 17:48 Comment(0)
G
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.

Garwood answered 29/12, 2016 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.