gpt4 book ai didi

objective-c - 如何在 ios 8 中获取当前位置的经纬度?

转载 作者:太空狗 更新时间:2023-10-30 03:34:11 25 4
gpt4 key购买 nike

我的问题不是在 ios 8 中获取当前位置的纬度和经度。我尝试在 ios 8 的 .plist 文件中设置 key ,但没有调用此方法 - didUpdateToLocation:请帮助解决此问题。

我的代码是:-

- (void)viewDidLoad

{

[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

if(IS_OS_8_OR_LATER) {
//[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
}

最佳答案

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
-(void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;
if(IS_OS_8_OR_LATER){
NSUInteger code = [CLLocationManager authorizationStatus];
if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
// choose one request according to your business.
if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
[self.locationManager requestAlwaysAuthorization];
} else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
}
}
}
[self.locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate
- (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];
}
}

In iOS 8 you need to do two extra things to get location working: Adda key to your Info.plist and request authorization from the locationmanager asking it to start. There are two Info.plist keys for the newlocation authorization. One or both of these keys is required. Ifneither of the keys are there, you can call startUpdatingLocation butthe location manager won’t actually start. It won’t send a failuremessage to the delegate either (since it never started, it can’tfail). It will also fail if you add one or both of the keys but forgetto explicitly request authorization. So the first thing you need to dois to add one or both of the following keys to your Info.plist file:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

这两个键都带一个字符串

which is a description of why you need location services. You canenter a string like “Location is required to find out where you are”which, as in iOS 7, can be localized in the InfoPlist.strings file.

enter image description here

关于objective-c - 如何在 ios 8 中获取当前位置的经纬度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26134641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com