简体   繁体   中英

startUpdatingLocation not being fired in iOS

I'm trying to write a simple geolocation app that find the users position in iOS. When the user presses a button, the user's position is found and displayed, and should be changing when startUpdatingLocation is fired. But startUpdatingLocation isn't being fired. Here's my code:

In my ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface CGViewController : UIViewController<CLLocationManagerDelegate>
@property (strong, nonatomic) IBOutlet UITextField *nameField;
- (IBAction)butStart:(id)sender;
@property (strong, nonatomic) IBOutlet MKMapView *map;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet UILabel *latitude;

@property (strong, nonatomic) IBOutlet UILabel *longitude;

@property (strong, nonatomic) IBOutlet UILabel *altitude;


@end

And in my ViewController.m:

- (IBAction)butStart:(id)sender {
    self.locationManager=[[CLLocationManager alloc] init];
    if([CLLocationManager locationServicesEnabled]){
        self.locationManager.delegate=self;
        self.locationManager.distanceFilter=1;
        [self.locationManager startUpdatingLocation];
    }
}

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog( @"new location = %@", [newLocation description] );
    MKCoordinateSpan span;
    span.latitudeDelta = 0.2;
    span.longitudeDelta = 0.2;

    MKCoordinateRegion region;
    region.span = span;
    region.center = newLocation.coordinate;
        map.showsUserLocation = YES;
        self.latitude.text = [NSString stringWithFormat:@"%.3f",   newLocation.coordinate.latitude];
    self.longitude.text = [NSString stringWithFormat:@"%.3f", newLocation.coordinate.longitude];
     }    

I've tried to leave out unnecessary code, like a map that I have. Every sample I've seen has the locationManager stuff being set in the AppDelegate.h/.m, whereas I'm trying to call it when the user presses a button. I'm not sure why its not being called.

The method which you have used is deprecated in SDK 6. You should use below method instead:

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations;

There are chances that if you are using iOS 6.0 and above the method which you have written may not get called.

I have also implement your code in my project and it is working fine .

-If you run your project in simulator first time then location service allow and disallow message comes and if you allow then this method get called please replace yoyr method with this

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

{

float currentLangitude = newLocation.coordinate.longitude;
float currentLatitude =  newLocation.coordinate.latitude;
NSLog( @"new location lat = %f long = %f", currentLangitude,currentLatitude );

}

thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM