簡體   English   中英

IOS 8 CLLocationManager問題(授權不起作用)

[英]IOS 8 CLLocationManager Issue (Authorization Not Working)

#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文檔,但是以某種方式我的代碼無法正常工作。

有人可以通過查看代碼中需要修改的地方來幫助我,使上述錯誤消息消失嗎?

謝謝!

在用戶授權您的應用使用位置服務之前,您不應該打開MKMapViewshowsUserLocation

您可以實現CLLocationManagerDelegatelocationManager:didChangeAuthorizationStatus:方法,並在showsUserLocation打開showsUserLocation ,如下所示:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {  
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        self.mapView.showsUserLocation = YES;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM