【问题标题】:fatal error: unexpectedly found nil while unwrapping an Optional value when getting Location?致命错误:在获取位置时展开可选值时意外发现 nil?
【发布时间】:2017-05-08 11:32:37
【问题描述】:

我正在使用 swift 开发一个应用程序我想获取用户的当前位置并希望在谷歌地图中显示所以我在我的课程中编写了我的代码我已经包含了所有的函数和方法(即添加和导入的框架核心)位置和更新的 plist,但有时我无法获取当前位置,但有时它会崩溃。

这是我尝试过的代码。

import CoreLocation
class HomeViewController: UIViewController, CLLocationManagerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.startUpdatingLocation()

    let location: CLLocation? = locationManager.location
    let coordinate: CLLocationCoordinate2D? = location?.coordinate  ----> App crossed this line then it will crashed
    print(coordinate!)
    print(coordinate!.latitude)
    print(coordinate!.longitude)
    strForCurLatitude = "\(coordinate!.latitude)"
    strForCurLongitude = "\(coordinate!.longitude)"}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedWhenInUse {
        print("User allowed us to access location")
    }
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Error while get location \(error)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    locationManager.startUpdatingLocation()
    let locationNew = locations.last
    let coordinateNew: CLLocationCoordinate2D? = locationNew?.coordinate
    strForCurLatitude = "\(coordinateNew!.latitude)"
    strForCurLongitude = "\(coordinateNew!.longitude)"
    strForLat=strForCurLatitude;
    strForLong=strForCurLongitude;
    print("User's Latitude is: \(Double(strForLat!)!)")
    print("User's Longitude is: \(Double(strForLong!)!)")
    self.locationManager.startUpdatingLocation()
}

【问题讨论】:

  • locationManager 需要时间来更新位置,如果你在viewDidLoad 尝试,那么你怎么能得到它?,在didUpdateLocations 你得到当前位置
  • 不要尝试在 viewDidLoad 方法中获取位置。他们已经给出了 didUpadteLocation 委托方法
  • 那么请告诉我在哪里。
  • 使用func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  • 好的,谢谢,我会试试这个。

标签: ios swift


【解决方案1】:

从 ViewDidLoad 中删除它,因为您无法在 viewDidLoad 中获取位置。

let location: CLLocation? = locationManager.location
    let coordinate: CLLocationCoordinate2D? = location?.coordinate 
    print(coordinate!)
    print(coordinate!.latitude)
    print(coordinate!.longitude)
    strForCurLatitude = "\(coordinate!.latitude)"
    strForCurLongitude = "\(coordinate!.longitude)"

在 CLLocationManager 的以下最适合获取位置的方法

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

【讨论】:

  • 好的,谢谢,我会试试的。
  • 您需要在google中显示多个注释或单个注释?
  • 单个注释
  • 这个用户是当前位置吗?
【解决方案2】:

将 CLLocationManager 引用为变量,如下所示:

class HomeViewController {

    var locationManager = CLLocationManager()

    viewDidLoad() {
       super.viewDidLoad()
    }
    // rest of code
}

在这种情况下Location Manager 应该可以正常工作。

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 2016-02-29
    • 2020-09-19
    • 2016-01-08
    相关资源
    最近更新 更多