如何在iOS 7中的简单ViewController中获取用户位置? 我试图以这种方式,但在调试我看到didUpdateLocation
方法不叫:在iOS 7中的简单ViewController中获取用户位置(didUpdateLocation)
在MyViewController.h:
#import <CoreLocation/CoreLocation.h>
@interface MyViewController : UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
在MyViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *newLocation = [locations lastObject];
NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Failed %ld",(long)[error code]);
}
didUpdateLocations
ISN没有打电话,但在日志中打印时被称为didFailWithError
:“失败0”。请帮帮我!
你有没有检查过[CLLocationManager locationServicesEnabled]和[CLLocationManager authorizationStatus]? – neilco
是 [CLLocationManager locationServicesEnabled]是1和 [CLLocationManager authorizationStatus]是3 – Pinturikkio
根据'CLError.h'错误代码0是'kCLErrorLocationUnknown'。你是在设备上还是在模拟器上运行它? – neilco