Ios 致命错误:获取位置时展开可选值时意外发现nil?

Ios 致命错误:获取位置时展开可选值时意外发现nil?,ios,swift,Ios,Swift,我正在使用swift开发一个应用程序,我想获取用户的当前位置,并希望在google地图中显示,因此我在我的类中编写了我的代码,我包含了所有函数和方法(即添加和导入了框架核心位置,还更新了plist,但我无法在获取位置时循环获取当前位置,但有一段时间它崩溃了 这是我尝试的代码 import CoreLocation class HomeViewController: UIViewController, CLLocationManagerDelegate { override func viewD

我正在使用swift开发一个应用程序,我想获取用户的当前位置,并希望在google地图中显示,因此我在我的类中编写了我的代码,我包含了所有函数和方法(即添加和导入了框架核心位置,还更新了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()
}

从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]) {
}

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

class HomeViewController {

    var locationManager = CLLocationManager()

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

在这种情况下,
位置管理器
应该可以正常工作。

位置管理器
需要时间来更新位置,如果您尝试在
viewDidLoad
中,那么如何获取位置?
diddupdatelocations
中获取当前位置请勿尝试在viewDidLoad方法中获取位置。他们已经给出了didUpadteLocation委托方法,请告诉我。使用
func locationManager(uManager:CLLocationManager,didUpdateLocations locations:[CLLocation]){
ok谢谢,我会试试。ok谢谢,我会试试。你需要在google中显示多个注释或单个注释吗?单个注释是这个用户当前的位置吗?