Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在第一次加载时请求授权后,viewDidLoad的其余部分为';t执行,下一行跳转到委托函数(swift 3)_Swift_Google Maps_Core Location_Cllocationmanager - Fatal编程技术网

在第一次加载时请求授权后,viewDidLoad的其余部分为';t执行,下一行跳转到委托函数(swift 3)

在第一次加载时请求授权后,viewDidLoad的其余部分为';t执行,下一行跳转到委托函数(swift 3),swift,google-maps,core-location,cllocationmanager,Swift,Google Maps,Core Location,Cllocationmanager,我的应用程序似乎在每次使用中都能正常工作,除了第一次。 我请求用户授权,并且在plist中有相应的密钥,但是请求授权的行之后的viewDidLoad的其余部分不会执行。我已经附加了下面的断点,在第一次使用应用程序时,断点2没有命中 我敢肯定,在授予授权后,它会跳转到扩展中的func locationManager 我可以等到最后才请求授权,直到其他一切都安排好,但不确定这是最好的,还是唯一的出路 谢谢 class MapController: UIViewController, GMSMapV

我的应用程序似乎在每次使用中都能正常工作,除了第一次。 我请求用户授权,并且在plist中有相应的密钥,但是请求授权的行之后的viewDidLoad的其余部分不会执行。我已经附加了下面的断点,在第一次使用应用程序时,断点2没有命中

我敢肯定,在授予授权后,它会跳转到扩展中的func locationManager

我可以等到最后才请求授权,直到其他一切都安排好,但不确定这是最好的,还是唯一的出路

谢谢

 class MapController: UIViewController, GMSMapViewDelegate {

    var locationManager = CLLocationManager()
    var currentLocation: CLLocation?  

    @IBOutlet var mapView: GMSMapView!

    override func viewDidLoad(){
        super.viewDidLoad()

        locationManager = CLLocationManager()
--------------------------> breakpoint 1
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
-------------------------> breakpoint 2
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 50
        locationManager.startUpdatingLocation()
        locationManager.delegate = self

        guard let lat = locationManager.location?.coordinate.latitude else {return}
        guard let lng = locationManager.location?.coordinate.longitude else {return}

        mapView.settings.compassButton = true
        mapView.settings.myLocationButton = true
        mapView.isMyLocationEnabled = true

        let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lng, zoom: 1)            
        mapView.camera = camera            
        mapView.delegate = self
          getData()
    }

    extension MapController: CLLocationManagerDelegate {

        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

            guard let location: CLLocation = locations.last else {return}     
            let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: 1)

            mapView.animate(to: camera)    
        }
}

你为什么要求授权两件不同的事情?如果您请求并获得always授权,您不需要在使用授权时请求,因为这只是always授权的一个子集

此外,这两个函数都是异步函数,因此您不能在它们之后执行基于位置的代码,因为如果您尚未获得授权,则
requestAuthorization()
之后的代码将在您实际获得授权之前执行,因此不会调用函数,因为您尚未获得授权


在调用任何与位置相关的代码(如
locationManager.startUpdatingLocation()
)之前,您必须检查授权状态,并且仅在状态已授权的情况下执行与位置相关的代码。如果它未经授权,您必须实现
CLLocationManagerDelegate
locationManager(\uuuxchangeAuthorization:)
函数,并在检查更改结果是否为授权状态后调用该函数中与位置相关的调用。

这是我不太明白的。如果请求授权是异步的,那么为什么viewDidLoad的其余部分不执行?如果是异步的,那么下一行location.startUpdatingLocation()不应该执行anywawy吗?您能解释一下为什么在我的示例中,尽管我没有startUpdatingLocations(),但仍然调用了locationManager委托didUpdateLocations吗?最后,如果有一个委托函数didChangeAuthorization,为什么该函数中的所有代码都不等待授权的更改呢?谢谢。正如我所说,它不会执行,因为在定义授权状态之前,您不能调用基于位置的函数。如果从未调用
startUpdatingLocations
,则无法调用委托函数,因此如果调用委托,则必须在代码中调用
startUpdatingLocations
didChangeAuthorization
仅在您的应用程序第一次启动时相关,或者如果用户在“设置”中更改授权状态,则不会再次调用此函数。我的设置类似于
manager.requestAlwaysAuthorization()manager.startUpdatingLocation()
你的意思是,在我没有点击“允许”之前……我不会得到任何更新,但在点击后我会得到更新,还是因为
startUpdatingLocation
在我们还没有点击“允许”的时候会立即得到评估,但它会自动完全失败?谢谢你的回复David。但是我有一个关于你最初的回答的问题,你说授权功能是异步的。我认为,如果它们是异步的,而RequestAuthorization在后台执行它的操作(无论是否允许),viewDidLoad中的所有其他内容都应该继续执行,例如mapView.isMyLocationEnabled=true和getData()末尾的函数。但事实并非如此。在应用程序的第一次运行期间从未访问getData。在谷歌的ios文档中,他们谈到在loadView中设置mapView…你这样做了吗?谢谢你的建议你可能想看到答案上的评论。。。