In this example Swift code you will learn how to get the current location of the user using CLLocationManager and CLLocationManagerDelegate. Below we look at a sample code:

  • Ask the user’s permission so that your application can always read the user’s location. You can use requestWhenInUseAuthorization() instead of requestAlwaysAuthorization(),
  • Update Info.plist projects with string values for the NSLocationAlwaysUsageDescription and NSLocationWhenWhenInUseUsageDescription dialogs,
  • Implement the locationManager didUpdateLocations function to continuously wait for location updates and call stopUpdatingLocation() to stop waiting for location updates,
  • Determines the current position of the user in latitude and longitude.

Factsheet

In order for your application to be able to retrieve the current location of the user, you need to open the Info.plist file as source code and add the two new keys and their corresponding values, as I did for example:

NSLocationAlwaysUsageDescription
Do you always want to know your location with this application?
NSL LocalizationWhenWhenWhen using
Do you tell this application your current location?

Determine the current location of the user. Full example in Swift

Import UIKit
CoreLocation
class ViewController : UIViewController, CLLocationManagerDelegate.

var locationManager:CLLocationManager!

Override viewDidLoad() {
super.viewDidLoad()
// Make any additional settings after loading the view, usually from the pen.
}

Override didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Remove all resources that can be recreated.
}

Replace the viewWillAppear(_ animated : Bool) {
super.viewWillAppear(animated))

DefinitionMyCurrentLocation()
}

func defineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}

locationManager(_ manager : CLLocationManager, didUpdateLocations location : [CLLocation]) enable [
lets userLocation:CLLocation = locations[0] as CLLocation.

// Call stopUpdatingLocation() to stop waiting for location updates,
// otherwise this function will be called every time the user changes location.

// manager.stopUpdatingLocation()

print (user latitude = (userLocation.coordinate.latitude))
print (user longitude = (userLocation.coordinate.longitude))
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print(Error)
}
}

For more information about the Swift code examples and tutorials, see the Swift code examples page on this site.

Determine User’s Current Location Example in Swift

Written by Sergei Kargopolov

get current location swiftui,didupdatelocations not called swift,get current location xcode 11,swift requestlocation example,get current location google maps ios swift,how to get current location address in swift,cllocation get longitude latitude,swift get lat lon,swift 5 get latitude and longitude,how to find latitude and longitude in swift,swift get user location once,swift get user country,get user location swiftui,get location in background ios swift,swift get geo location,mapkit current location,update user location swift,show current location on google map ios swift,requestlocation swift,isauthorizedforpreciselocation,startupdatinglocation swift,corelocation tutorial,swift location tracking,standard location service swift,ios visits,swiftui get location,core location coordinates,apple significant change location service,swift 4 get user current location,show current location on map ios swift,get user location in swift 4,how to get current location latitude and longitude in swift 4,xcode swift get user current location,get latitude and longitude from address swift 4,how to get current location latitude and longitude in swift 5,how to set current location in swift 4

You May Also Like

Analyzing Android Project with Lint and SonarQube –

Development issue/problem: I’ve been very busy making the connection between these things.…

The Best Cell Phones For Sale Without Plans

If you want to buy the best contract-free plans from a reputable…

How to Configure Block Storage on Linux

It is very important to understand and manage data storage under Linux.…

How to fix Wi-Fi range issues on Windows 10

DriverFix’s built-in library ensures that all downloaded and installed drivers are the…