我一直在試圖獲取位置座標。我撥打[locationManager startUpdatingLocation];
後沒有任何反應。iOS startUpdatingLocation從未調用
它永遠不會調用- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
有什麼想法?
說明:我已設置CLLocationManagerDelegate
並將CLLocationManager
設置爲strong
。在我的viewDidLoad
中,我設置了我的代表,並且正在嘗試使用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
我也有特里結構d刪除[locationManager stopUpdatingLocation];
,它仍然沒有做任何事情。
其他注意事項:如果我檢查設備設置/隱私,它表示我的應用確實具有位置權限。我所有的.h
屬性都是@synthesize
正確在.m
文件中。
我'[的LocationManager requestAlwaysAuthorization]' – erad 2014-09-24 12:29:21
我的問題是,我需要添加'NSLocationAlwaysUsageDescription'到' .plist'。 我現在正在獲取位置數據。 (雖然不正確的數據,但我懷疑這是一個模擬器的東西。) 非常感謝。 – erad 2014-09-24 12:37:20
位置管理器在當前(Xcode 6.0.1)模擬器中非常破碎。 – EricS 2014-09-24 13:28:41