Objective c gps 坐标不工作

Objective c gps coordinates not working

我遵循了很多教程,但 none 有效。坐标始终输出0.000000, 0.000000.

这是我的位置代码:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateLocations:(NSArray *)locations
{
    currentLocation = (CLLocation *)[locations lastObject];

}
-(void)getLocation {
    longitude = floorf(currentLocation.coordinate.longitude * 10000)/10000;

}

我有一个按钮:

locationManager stopUpdatingLocation];
[self getLocation];

我在 viewDidLoad 中有这个:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];

我在 xcode 中的 ios 模拟器中使用了一个假位置,所以它不应该输出 0.000000。

您可能需要在 viewDidLoad 中添加这一行:

[locationManager requestAlwaysAuthorization];

如果这不起作用,我倾向于说这是因为您在模拟器中 运行ning。甚至假地点也一度让我搞砸了。你可以 运行 在设备上吗?

编辑:尝试将此添加到 info.plist:

<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>

按照本教程,您找到了纬度和经度,并通过使用这两种方法还获得了地址...:)

https://www.appcoda.com/how-to-get-current-location-iphone-user/

您需要申请授权才能使用定位服务。

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    [self.locationManager requestAlwaysAuthorization];
}