簡體   English   中英

如何修復線程 1:致命錯誤:在隱式展開可選值時意外發現 nil?

[英]How to fix Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value?

我對用戶位置有疑問。

當我嘗試構建程序時,它會收到以下錯誤代碼:(線程 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 function =:

  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)
    }

您可以根據需要在任何地方調用它,請記住在請求許可時打開位置,如果在模擬器 go 中使用到功能/位置/自定義位置

我現在有另一個代碼,它比舊代碼短。

這是代碼:

導入 UIKit 導入 MapKit 導入 CoreLocation

class 視圖控制器: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
    }

}

現在它遇到了與舊問題相同的問題:(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM