CLLocationManager EXC_BAD_INSTRUCTION

CLLocationManager EXC_BAD_INSTRUCTION

我正在为我的应用程序使用 GoogleMaps,并且我使用 requestWhenInUseAuthorization 正确设置了 Info.plistlocationManager...didChangeAuthorization 中的所有内容,但我仍然遇到错误

EXC_BAD_INSTRUCTION

即使我设置正确。下面是我的代码。

locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
let camera = GMSCameraPosition.camera(withTarget: (self.locationManager.location?.coordinate)!, zoom: zoomLevel)
self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = self.mapView
self.initializeTheLocationManager()
self.mapView.isMyLocationEnabled = true

这是我的 locationManager 委托

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedAlways || status == .authorizedWhenInUse {
        mapView.delegate = self
        manager.startUpdatingLocation()
    } else {
        manager.requestWhenInUseAuthorization()
    }
}

注意: requestWhenInUseAuthorization.

好像我请求了两次

这行不通。

startUpdatingLocation 工作 异步 。你必须实施 didUpdateLocations.

    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

...

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let camera = GMSCameraPosition.camera(withTarget: (locations[0].coordinate, zoom: zoomLevel)
    self.locationManager.stopUpdatingLocation()
    self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    self.view = self.mapView
    self.initializeTheLocationManager()
    self.mapView.isMyLocationEnabled = true
}