【问题标题】:How to fix Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value?如何修复线程 1:致命错误:在隐式展开可选值时意外发现 nil?
【发布时间】:2021-02-06 16:30:32
【问题描述】:

我的用户位置有问题。

当我尝试构建程序时,它会收到以下错误代码:(线程 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)
        }
    }
}

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

提前谢谢你。

【问题讨论】:

  • 一个可能的原因是 mapView = nil 因为故事板中的插座未正确连接。这是stackoverflow.com/q/32170456/1187415中的解释之一。
  • 尝试在 viewDidAppear() 方法中而不是在 viewDidLoad() 中添加 configerLocationServices()
  • 这没有帮助:(

标签: ios swift xcode mkmapview xcode11.4


【解决方案1】:
 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)
    }

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

【讨论】:

  • 我还是不明白:(
  • @BlueDynamixs 如果您可以与我分享您的课程,我将随时为您提供帮助
  • 没关系,我刚刚学习了另一个教程 :)
【解决方案2】:

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

代码如下:

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

类视图控制器: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
    }

}

现在它遇到了与旧问题相同的问题:(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2021-01-05
    • 1970-01-01
    • 2020-02-03
    • 2020-11-01
    • 2022-07-07
    相关资源
    最近更新 更多