Get current Location Latitude and Longitude in swift

Set location permission in your info.plist file

NSLocationAlwaysAndWhenInUseUsageDescription
Fetching current country and city.

NSLocationUsageDescription
Fetching current country and city.

NSLocationWhenInUseUsageDescription
Fetching current country and city.

Import following library and set variable

import CoreLocation

var locationManager = CLLocationManager()

Use following method to start update location

 

func startLocationManager() {
        // always good habit to check if locationServicesEnabled
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.distanceFilter = kCLDistanceFilterNone
            locationManager.startMonitoringSignificantLocationChanges()
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
        }
    }

Implement locationManager delegate's method

 

extension ViewController:CLLocationManagerDelegate{
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        let location = locations.last
        currentCoordinate = (location?.coordinate)!
        let currentlocation = CLLocation(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        print(currentlocation)
       
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }
}

Stop updating Location

self.locationManager.stopUpdatingLocation()

Note: didUpdateLocations delegate method are called continue and get updated latitude longitude for your current location. if you stop call this method for use stopUpdatingLocation() method.