User's Current Location and Coordinates

About User's Current Location and Coordinates Documentation

Imagem de capa


Import MapKit + CoreLocation

adding CLLocationManagerDelegate in the class definition.

To get a user’s current location you need to declare:

let locationManager = CLLocationManager()

In viewDidLoad() you have to instanitate the CLLocationManager class, like this:

// Ask for Authorisation from the User.

// For use in foreground

if CLLocationManager.locationServicesEnabled() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters

Then in CLLocationManagerDelegate method you can get user’s current location coordinates:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = \(locValue.latitude) \(locValue.longitude)")

In the info.plist you will have to add Privacy - Location Always Usage Description and your custom alert message like, AppName(Demo App) would like to use your current location.

Want to see something else added? Open an issue.