I'm currently trying to create a heat map using the googlemaps sdk, but when attempting to retrieve the user location, I receive the following error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value".
I've pasted my code down below and was hoping someone could take a quick look over it to identify why this may be the case. I've also highlighted the nil value (//NIL VALUE) which is meant to set the users current coordinates as a variable.
import GoogleMaps
import SwiftUI
import UIKit
import GoogleMapsUtils
class MapViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
private var mapView: GMSMapView! //Default
private var list = [GMUWeightedLatLng]()
private var heatmapLayer: GMUHeatmapTileLayer!
//Colour gradient
private var gradientColors = [UIColor.green, UIColor.red]
private var gradientStartPoint = [0.2, 1.0] as [NSNumber]
let locationManager = CLLocationManager()
override func loadView() {
super.loadView() //LoadMap - Default
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
let myLoc = locationManager.location?.coordinate //NIL VALUE
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
//Camera Focus
let camera = GMSCameraPosition.camera(withLatitude: myLoc!.latitude, longitude: myLoc!.longitude, zoom: 15.0)
mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
mapView.delegate = self
mapView.isMyLocationEnabled = true
self.view = mapView
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Any advice on how to fix this issue would be greatly appreciated!