没有获取用户位置

Not getting user location

我需要获取用户的位置。

我已经包含了 info.plist

中需要的所有三个密钥

我在视图控制器中声明了变量:

       // Used to start getting the users location
var locationManager = CLLocationManager()
var currentLocation: CLLocation!

我用 CLLocationManagerDelegate

扩展了视图控制器
class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UISearchBarDelegate, CLLocationManagerDelegate

并且我已将此代码包含在 viewDidLoad 中:

  locationManager.requestWhenInUseAuthorization()


    if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse) || (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {

                currentLocation = locationManager.location
                print(currentLocation.coordinate.latitude)
                print(currentLocation.coordinate.longitude)

            }

请求用户许可,但未收到坐标。

我做错了什么?

您必须调用 startupdatinglocation() https://developer.apple.com/documentation/corelocation/cllocationmanager/1423750-startupdatinglocation 才能接收 CLLocationManagerDelegate

的消息

您可以查看 https://fluffy.es/current-location/ 以获得完整的教程