0

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!

Asperi
  • 228,894
  • 20
  • 464
  • 690
Skyrifter
  • 1
  • 1
  • 1
    You likely shouldn't be trying to read the location here. Instead, you should be listening to the delegate methods: https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate – jnpdx Jul 08 '22 at 02:17
  • It is surprising that you get that exact crash, though -- are you sure you don't have `let myLoc = locationManager.location!.coordinate` and it's crashing there? – jnpdx Jul 08 '22 at 02:18
  • Hey, thanks for the reply! The code i attached is the exact code i'm using, it's showing the "thread 1: fatal error" on the "let camera" line. I had a read through the documentation you added, but I was still unsure as to where I should be reading it if not there sorry. – Skyrifter Jul 08 '22 at 03:22
  • Ah, I thought the error was at `//NIL VALUE`. In that case, it's very clear why you're getting the error -> `myLoc` is `nil`. See https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu – jnpdx Jul 08 '22 at 03:24
  • Also, the tutorial i'm referencing to create this project has it listed exactly as I do, with no errors which is why i was really confused about the situation – Skyrifter Jul 08 '22 at 03:24
  • If you have a tutorial that shows this code, you shouldn't use it -- it is extremely unsafe to unwrap an optional like this with `!`. Check this out for a more reliable source about how to get location: https://www.advancedswift.com/user-location-in-swift/ – jnpdx Jul 08 '22 at 03:25

0 Answers0