Not sure what the issue is as this code ran fine a week ago and ive made no changes.
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates! )
//gives the following
///Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
//the full code for the view
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
WOLMenu.target = self.revealViewController()
WOLMenu.action = #selector(SWRevealViewController.revealToggle(_:))
WOLMapView.delegate = self
WOLMapView.showsScale = true
WOLMapView.showsPointsOfInterest = true
WOLMapView.showsUserLocation = true
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
let sourceCoordinates = locationManager.location?.coordinate
let destCoordinates = CLLocationCoordinate2DMake( 32.4208, -104.2400)
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates! )
let destPlacemark = MKPlacemark(coordinate: destCoordinates)
let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceItem
directionRequest.destination = destItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler: {
response, error in
guard let response = response else {
if let error = error {
print("Something went wrong")
}
return
}
let route = response.routes[0]
self.WOLMapView.add(route.polyline, level: .aboveRoads)
let rekt = route.polyline.boundingMapRect
self.WOLMapView.setRegion(MKCoordinateRegionForMapRect(rekt), animated: true)
})
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.black
renderer.lineWidth = 5.0
return renderer
}
override var prefersStatusBarHidden: Bool {
return true
}
}