0
import UIKit
import MapKit
import CoreLocation

class GetLocation: UIViewController, CLLocationManagerDelegate {
    
//    var isLocationEnabled = false
    
    var locationManager: CLLocationManager!
    
    var lat: Double = 1
    var lon: Double = 1
    
    var region = CLLocation(latitude: lat, longitude: lon)
  
    //    let geoCoder = CLGeocoder()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.locationManager = CLLocationManager()
        self.locationManager?.delegate = self
       
    }
    
    func regionManager() {
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestAlwaysAuthorization()
        
        if CLLocationManager.locationServicesEnabled() {
            print("Location Enabled")
        } else {
            print("Location Not Enabled")
        }
    }
   
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        let location = locations[0] as CLLocation
        
        let latitube = location.coordinate.latitude
        let longitube = location.coordinate.longitude
        
        lat = latitube
        lon = longitube
    }
}

enter image description here

I want to create a property with CLLocation's longitude and latitude. I tried adding lazy before the variable. There is no error, but the longitude and latitude results are not applied to the attributes.

I made a func and tried to use it but it's no use. It just throws an error saying it found nil.

What is the problem? I tried to solve it by myself for a long time, but It is too difficult for me who started studying. I need your kind help.

CLLocation in Swift please help.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
mgynsz
  • 1
  • 1
  • If you're using latitude and longitude it necessarily would have to break down into two quantities. However your intuition is correct that location really is a singular quantity. People have solved this issue by developing what's known as geoHash's which break the entire world down into little patches that fit together (source: https://www.youtube.com/watch?v=T5q_zLk_4s8). I hope this helps. – jimBeaux27 Dec 29 '22 at 23:31

0 Answers0