IOS 8 CLLocationManager问题(授权不起作用)

时间:2014-08-09 19:23:00

标签: ios 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文件(我做了),并为说明添加了一个字符串。

但是,我在实施授权部分时遇到问题,因为错误仍然如此:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

我已经为CLLocationManager阅读了Apple IOS 8文档,但不知怎的,我的代码无效。

有人可以帮助我,以便通过查看我需要修改的代码中的哪些内容来解决上述错误消息,以便它能够正常工作吗?

谢谢!

1 个答案:

答案 0 :(得分:5)

在用户授权您的应用使用位置服务之前,您不应该启用MKMapView showsUserLocation

您可以实施CLLocationManagerDelegate的{​​{1}}方法并在其中启用locationManager:didChangeAuthorizationStatus:,如下所示:

showsUserLocation