簡體   English   中英

iOS startUpdatingLocation從未調用

[英]iOS startUpdatingLocation never called

我一直在嘗試獲取位置坐標。 在我調用[locationManager startUpdatingLocation]; 什么都沒發生。

它從不調用- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

有什么想法嗎?

注意:我已經設置了CLLocationManagerDelegate ,並將我的CLLocationManager類型設置為strong 在我的viewDidLoad我設置了Delegate,並且嘗試使用requestAlwaysAuthorization

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO");
    NSLog(@"locationManager init");
    NSLog(@"geocoder init");
    locationManager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];

    //locationManager.delegate = self;
    [locationManager setDelegate:self];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager requestAlwaysAuthorization];

    NSLog(@"locationManager startUpdatingLocation");
    [locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"locationManager:manager");
    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
     didUpdateLocations:(NSArray *)locations;
{
    NSLog(@"locations %@", locations);

    CLLocation *newLocation = [locations lastObject];
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

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

    // Stop Location Manager
    [locationManager stopUpdatingLocation];

    // Reverse Geocoding
    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];
            locationLabel.text = [NSString stringWithFormat:@"%@, %@",
                                 placemark.locality,
                                 placemark.administrativeArea];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLGeocoder *geocoder;
@property (strong, nonatomic) CLPlacemark *placemark;

日志記錄

2014-09-24 08:18:39.783 appName[1201:130186] locationServicesEnabled: YES
2014-09-24 08:18:39.784 appName[1201:130186] locationManager init
2014-09-24 08:18:39.784 appName[1201:130186] geocoder init
2014-09-24 08:18:39.785 appName[1201:130186] locationManager startUpdatingLocation

我也嘗試過刪除[locationManager stopUpdatingLocation]; 而且它仍然什么也沒做。

其他說明:如果我檢查設備設置/隱私,則表明我的應用確實具有位置權限。 我的所有.h屬性在.m文件中都正確@synthesize

如果您使用的是iOS 8,則需要在調用[locationManager startUpdatingLocation];之前調用以下方法之一: -[CLLocationManager requestWhenInUseAuthorization]-[CLLocationManager requestAlwaysAuthorization] [locationManager startUpdatingLocation]; 第一。

您還需要在.plist中為這些鍵NSLocationUsageDescriptionNSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

暫無
暫無

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

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