【发布时间】:2015-05-30 02:35:21
【问题描述】:
我正在开发的 Xcode 应用程序需要获取用户 iOS 设备的纬度和经度。虽然目前我得到的两个值都是 0。它不要求获得该位置的许可,而且我使用的是真实设备而不是模拟器。
NSLocationAlwaysUsageDescription 在我的 info.plist 中
我还导入了 CoreLocation 并在我的 .h 文件中
@interface LocationViewController : UIViewController <CLLocationManagerDelegate>
在我的 .m 文件中
@interface LocationViewController () <CLLocationManagerDelegate>
这是我的代码:
@implementation LocationViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self getCurrentLocation];
}
-(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
- (void)getCurrentLocation{
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSLog(@"Latitude = %@", latitude);
NSLog(@"Longitude = %@", longitude);
}
【问题讨论】:
-
哪个iOS?我相信您正在测试 iOS 8 及更高版本...对吗?
-
是的 iOS 8.2,谢谢 :)