2013-12-16 39 views
-1

如何在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”。请帮帮我!

+0

你有没有检查过[CLLocationManager locationServicesEnabled]和[CLLocationManager authorizationStatus]? – neilco

+0

是 [CLLocationManager locationServicesEnabled]是1和 [CLLocationManager authorizationStatus]是3 – Pinturikkio

+0

根据'CLError.h'错误代码0是'kCLErrorLocationUnknown'。你是在设备上还是在模拟器上运行它? – neilco

回答

2

首先检查你确实有一个有效的WiFi和3G连接

然后

1)进入设置和重置定位服务

2)重置您的网络设置

而且如果它在模拟器中,则检查是默认位置集。 This链接将显示。

1

CLLocation经理说

kCLAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application 
kCLAuthorizationStatusRestricted,  // This application is not authorized to use location services. Due 
             // to active restrictions on location services, the user cannot change 
             // this status, and may not have personally denied authorization 
kCLAuthorizationStatusDenied,   // User has explicitly denied authorization for this application, or 
             // location services are disabled in Settings 
kCLAuthorizationStatusAuthorized   // User has authorized this application to use location services 

在你的情况CLAuthorizationStatus 0意味着你不会允许应用程序访问定位服务。

+0

我的案例中的CLAuthorizationStatus是3! – Pinturikkio