【问题标题】:CLLocationManager EXC_BAD_INSTRUCTIONCLLocationManager EXC_BAD_INSTRUCTION
【发布时间】:2019-01-15 08:26:45
【问题描述】:

我将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

【问题讨论】:

  • 到底是在哪里崩溃的?
  • 你会遇到什么问题?
  • @mag_zbc this self.locationManager.location?.coordinate)! 它返回 nil 尽管我已经允许模拟器请求位置。
  • 在模拟器中,您无法获取当前位置。您应该为此使用真实设备
  • @AlvinQuezon 您是否已将自定义位置添加到您的模拟器?

标签: ios swift google-maps cllocationmanager google-maps-sdk-ios


【解决方案1】:

这行不通。

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多