【问题标题】:Location Manager in IOS 8 Not working [duplicate]IOS 8中的位置管理器不起作用[重复]
【发布时间】:2015-02-23 13:09:48
【问题描述】:

我尝试显示用户的当前位置(我主要需要邮政编码),当我启动应用程序时没有任何反应,应用程序只显示空标签而没有崩溃消息。 这是我的代码:

@implementation ViewController {
    CLLocationManager *locationManager;
    CLGeocoder *geocoder;
    CLPlacemark *placemark;
}

@synthesize array, mytableView, latitudeLabel, longitudeLabel, addressLabel;

- (void)viewDidLoad {
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
        [locationManager requestWhenInUseAuthorization];
    }
    [locationManager startUpdatingLocation];
    geocoder = [[CLGeocoder alloc] init];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }

    // Stop Location Manager
    [locationManager stopUpdatingLocation];

    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            addressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                                 placemark.subThoroughfare, placemark.thoroughfare,
                                 placemark.postalCode, placemark.locality,
                                 placemark.administrativeArea,
                                 placemark.country];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

- (IBAction)getCurrentLocation:(id)sender {
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];

}

【问题讨论】:

  • 您是否将 plist 设置为允许位置授权?
  • 我的 plist 中没有任何内容,所以我没有使用它。
  • 您需要更新您的 plist:stackoverflow.com/a/24063578/2274694
  • 它给了我经度和纬度,但没有给我地址。那是因为它是在模拟器上运行而不是在实际设备上运行的吗?

标签: ios objective-c cllocationmanager


【解决方案1】:

在您的 plist 文件中添加这些属性。 string 的值可以是你想要的任何值。

已编辑以进一步回答评论中迈克的另一个问题:

在视图控制器的 .h 文件中

@interface YourViewController : UIViewController<CLLocationManagerDelegate>

【讨论】:

  • 我通过 locationManager.delegate = self 得到这个错误;说“从不兼容的类型 'ViewController *const_strong' 分配 'id'” 1-这是什么意思,我该如何解决?
  • 我认为您尚未将尝试获取位置的视图控制器注册为位置管理器的代表。在视图控制器中实现 协议。
  • 我不确定发生了什么,但是今天当我测试我的应用程序时,它给了我 Error Domain=kCLErrorDomain Code=0 "The operation could not be completed. (kCLErrorDomain error 0 .)"
猜你喜欢
  • 1970-01-01
  • 2013-01-01
  • 2014-08-01
  • 2015-04-25
  • 2014-11-16
  • 2014-09-12
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
相关资源
最近更新 更多