获取 iOS 上的当前位置
Get current location on iOS
我需要获取当前用户位置(纬度、经度),但我找不到解决方案,因为所有解决方案都只针对 iOS 6,7,8。例如,我有这段代码,但在 iOS 11.3.1 上它仍然无法正常工作。
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *latitudeValue;
@property (weak, nonatomic) IBOutlet UILabel *longtitudeValue;
@property (nonatomic,strong) CLLocationManager *locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
} else {
NSLog(@"Location services are not enabled");
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
self.latitudeValue.text = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
self.longtitudeValue.text = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
}
这是因为您需要在 self.locationManager 对象上调用这些 requestAlwaysAuthorization
或 requestWhenInUseAuthorization
方法之一,并在您的 App Plist 中相应地添加键。 Take a look.
从 iOS 10 你需要添加两个键到 info.plist as
NSLocationAlwaysUsageDescription key to your Info.plist file. (Xcode displays this key as "Privacy - Location Always Usage Description" in the Info.plist editor.)
而 iOS 11
Add the NSLocationWhenInUseUsageDescription key and the NSLocationAlwaysAndWhenInUseUsageDescription key to your Info.plist file. (Xcode displays these keys as "Privacy - Location When In Use Usage Description" and "Privacy - Location Always and When In Use Usage Description" in the Info.plist editor.)
也可以在苹果文档中找到完整的指南:apple corelocation authorization
我需要获取当前用户位置(纬度、经度),但我找不到解决方案,因为所有解决方案都只针对 iOS 6,7,8。例如,我有这段代码,但在 iOS 11.3.1 上它仍然无法正常工作。
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *latitudeValue;
@property (weak, nonatomic) IBOutlet UILabel *longtitudeValue;
@property (nonatomic,strong) CLLocationManager *locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
} else {
NSLog(@"Location services are not enabled");
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
self.latitudeValue.text = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
self.longtitudeValue.text = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
}
这是因为您需要在 self.locationManager 对象上调用这些 requestAlwaysAuthorization
或 requestWhenInUseAuthorization
方法之一,并在您的 App Plist 中相应地添加键。 Take a look.
从 iOS 10 你需要添加两个键到 info.plist as
NSLocationAlwaysUsageDescription key to your Info.plist file. (Xcode displays this key as "Privacy - Location Always Usage Description" in the Info.plist editor.)
而 iOS 11
Add the NSLocationWhenInUseUsageDescription key and the NSLocationAlwaysAndWhenInUseUsageDescription key to your Info.plist file. (Xcode displays these keys as "Privacy - Location When In Use Usage Description" and "Privacy - Location Always and When In Use Usage Description" in the Info.plist editor.)
也可以在苹果文档中找到完整的指南:apple corelocation authorization