CLLocation当前位置不起作用
我在我的类ViewController
有这样的代码:
CLLocationManager *locationManager; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. locationManager = [[CLLocationManager alloc] init]; [locationManager requestWhenInUseAuthorization]; } - (IBAction)getCurrentLocation:(id)sender { locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; } #pragma mark - CLLocationManagerDelegate -(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"Did finish with error - %@", error); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"did Update Location - %@", newLocation); CLLocation *currentLocation = newLocation; if(currentLocation != nil) { _longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; } }
但我没有获得允许访问的位置或popup窗口。
我正在使用核心位置框架。
在button上单击我打印标签中的纬度,经度和地址。
我正在模拟器上testing这个。
有时候,模拟器不能使用启用位置的服务使用Apple Device
进行完美的testing。
在您的info.plist
文件中添加以下密钥以允许访问popup窗口。
或者将它们添加为更新info.plist
文件的源代码。
<key>NSLocationAlwaysUsageDescription</key> <string>message for location uses</string> <key>NSLocationWhenInUseUsageDescription</key> <string>message for location uses</string>
试试这个代码在viewDidLoad …
//---- For getting current gps location locationManager = [CLLocationManager new]; locationManager.delegate = self; if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [locationManager requestWhenInUseAuthorization]; } locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; //------
您还必须将NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription键的string添加到应用程序的Info.plist中。
locationManager =[[CLLocationManager alloc] init]; [locationManager requestWhenInUseAuthorization]; locationManager.delegate=self; locationManager.desiredAccuracy=kCLLocationAccuracyBest; [locationManager startUpdatingLocation];
你可以添加你的plist:
<key>NSLocationAlwaysUsageDescription</key> <key>NSLocationWhenInUseUsageDescription</key>
- 从代码打开地图应用程序 – 在哪里/如何find“当前位置”?
- 位置pipe理器给出空坐标
- 为什么self.locationManager stopUpdatingLocation不停止位置更新
- 用户拒绝位置服务后再次请求权限?
- didFailWithError:错误域= kCLErrorDomain代码= 0“操作无法完成。 (kCLErrorDomain错误0.)“
- 蓝色横幅“您的应用正在使用您的位置”在退出我的应用后不显示
- 应用程序终止时,位置服务不会停止
- 什么位置经理提供最近的价值
- MyApp“具有超出允许时间的活动断言”崩溃 – CoreLocationRegistration和CoreLocationBackgroundClient