如何修复线程 1:致命错误:在隐式解包 Optional 值时意外发现 nil?

问题描述

我的用户位置有问题。

当我尝试构建程序时,它得到以下错误代码:(线程 1:致命错误:在隐式解包可选值时意外发现 nil)

我的代码



import UIKit
import MapKit
   


class ViewController: UIViewController {
    
    private let locationManager = CLLocationManager()
    private var currentCoordinate: CLLocationCoordinate2D?
    
    @IBOutlet weak var mapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        configerLocationServices()
    }
    
    private func configerLocationServices() {
        locationManager.delegate = self
        
        let status = CLLocationManager.authorizationStatus()
        
        if status == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
        } else if status == .authorizedAlways || status == .authorizedWhenInUse {
            beginLocationUpdates(locationManager: locationManager)
        }
    }
    
    private func beginLocationUpdates(locationManager: CLLocationManager) {
        mapView.showsUserLocation = true //<--- the problem is here
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
    }
    
    private func zoomToLastestLocation(with coordinate: CLLocationCoordinate2D) {
        let zoomregion = MKCoordinateRegion(center: coordinate,latitudinalMeters: 10000,longitudinalMeters: 10000)
        mapView.setRegion(zoomregion,animated: true)
    }
}

extension ViewController: CLLocationManagerDelegate {
    
    func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
        print("Did get latest Location")
        
        guard let latestLocation = locations.first else { return }
        
        if currentCoordinate == nil {
            zoomToLastestLocation(with: latestLocation.coordinate)
        }
            
        currentCoordinate = latestLocation.coordinate
        
    }
func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
        print("The Status changed")
        if status == .authorizedAlways || status == .authorizedWhenInUse {
            self.beginLocationUpdates(locationManager: manager)
        }
    }
}

我不知道我做错了什么,有没有人解决

提前致谢。

解决方法

 private func setNewLoaction(lat:CLLocationDegrees,long:CLLocationDegrees,markerTitle:String){
        
        let center = CLLocationCoordinate2D(latitude: lat,longitude: long)
        let camera = GMSCameraPosition.camera(withLatitude: lat,longitude: long,zoom: 15)
        self.googleMapsView?.camera = camera
        self.googleMapsView?.isMyLocationEnabled = true
        let marker = GMSMarker(position: center)
        marker.map = self.googleMapsView
        marker.title = markerTitle
        locationManager.stopUpdatingLocation()
        
    }
    
    //MARK:- MAP VIEW
    
    private func setUpMap(){
        
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
        self.googleMapsView = GMSMapView (frame: CGRect(x: 0,y: 0,width: self.view.frame.width-30,height: self.mapView.frame.height))
        self.googleMapsView?.settings.compassButton = true
        self.googleMapsView?.isMyLocationEnabled = true
        self.googleMapsView?.settings.myLocationButton = true
        self.mapView.addSubview(self.googleMapsView!)
    }

我在 ViewDidload 中调用了 setUpMap,在 GMSAutocompleteViewControllerDelegate 中调用了 setLoaction 函数 =:

  func viewController(_ viewController: GMSAutocompleteViewController,didAutocompleteWith place: GMSPlace) {
    
            
            destinationField.text = place.formattedAddress
            destinationLatitude = place.coordinate.latitude
            destinationLongitutude = place.coordinate.longitude
            setNewLoaction(lat:  destinationLatitude!,long: destinationLongitutude!,markerTitle: "Destination Location")
            
       
        dismiss(animated: true,completion: nil)
    }

您可以根据需要在任何地方调用它,请记住在请求许可时打开位置,如果在模拟器中使用,请转到功能/位置/自定义位置

,

我现在有一个相同的代码,它比旧代码短。

代码如下:

导入 UIKit 导入 MapKit 导入核心位置

class ViewController: UIViewController {

@IBOutlet var mapView: MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {
        super.viewDidLoad()
    
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.startUpdatingLocation()
    
        mapView.showsUserLocation = true // <--- Crash
    }

}

现在它遇到了和旧的一样的问题:(

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...