IOS 8 CLLocationManager问题(授权不起作用)
#import "MyLocationViewController.h"
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
@interface MyLocationViewController ()
@end
@implementation MyLocationViewController
{
CLLocationManager *locationManager;
}
- (void)requestAlwaysAuthorization
{
[locationManager requestAlwaysAuthorization];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
}
- (IBAction)unwindToMap:(UIStoryboardSegue *)segue
{
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.001;
mapRegion.span.longitudeDelta = 0.001;
CLLocationCoordinate2D location = userLocation.coordinate;
float lat = location.latitude;
float lng = location.longitude;
NSDictionary *locationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:lat] , @"Latitude",
[NSNumber numberWithFloat:lng], @"Longitude", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
locationDictionary, @"Location_A",
nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
if (mapView.userLocation != nil)
{
_longitudeLabel.text = [NSString stringWithFormat:@"%.8f", mapView.userLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", mapView.userLocation.coordinate.latitude];
}
[mapView setRegion:mapRegion animated: YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这是我的代码。
现在,我了解到我需要使用NSLocationAlwaysUsageDescription键编辑info.plist文件(该操作),并添加了描述字符串。
但是,我在实现授权部分时遇到了问题,因为错误仍然显示为:
However, I'm having issues implementing the authorization portion, as the error still reads as such:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
我已经阅读了CLLocationManager的Apple IOS 8文档,但是我的代码无法正常工作。
I've read the Apple IOS 8 docs for the CLLocationManager, but somehow my code will not work.
有人可以帮我看看上面我需要修改的代码以使上面的错误消息消失吗?
Can someone help me so that the above error message goes away by looking at where in my code I need to modify so that it works?
谢谢!
您不应打开 MKMapView
的 showsUserLocation
在用户授权您的应用使用位置服务之前。
You should not turn on MKMapView
's showsUserLocation
before the user has authorised your app to use Location services.
您可以实施 CLLocationManagerDelegate
的 locationManager:didChangeAuthorizationStatus:
方法并打开 showsUserLocation
像这样:
You can implement CLLocationManagerDelegate
's locationManager:didChangeAuthorizationStatus:
method and turn on showsUserLocation
there like so:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
self.mapView.showsUserLocation = YES;
}
}