未获得用户位置

问题描述 投票:0回答:1

我需要获取用户的位置。

我已经在info.plist中包含了所有三个键

enter image description here

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

       // 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)

            }

要求用户授予许可,但未收到坐标。

我在做什么错?

ios swift cllocationmanager
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.