# How to get the current location latitude and longitude in iOS?

Almost all the application uses location services, thus having complete understanding on location is necessary. In this post we will be seeing how to get current location’s latitude and longitude.

For this we will be using CLLocationManager, you can read more about it herehttps://developer.apple.com/documentation/corelocation/cllocationmanager

We will be developing a sample application where we will print user’s latitude and longitude on viewDidLoad method, alternatively you can print on tap of a button also on UILabel as per need.

So let’s get started,

Step 1 − Open Xcode → New Projecr → Single View Application → Let’s name it “Location”

Step 2 − Open info.plist file and add the below keys.

<key>NSLocationWhenInUseUsageDescription</key>

<string>Application wants to use your location</string>

<key>NSLocationAlwaysUsageDescription</key>

<string>Application wants to use your location</string>

These are required whenever you’re doing location related stuff, we need to ask user’s permission.

Step 3 − In ViewController.swift,

import CoreLocation

Step 4 − Create an Object of CLLocationManager

var locationManager = CLLocationManager()

Step 5 − In viewDidLoad method write the below code,

locationManager.requestWhenInUseAuthorization()
var currentLoc: CLLocation!
if(CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == .authorizedAlways) {
currentLoc = locationManager.location
print(currentLoc.coordinate.latitude)
print(currentLoc.coordinate.longitude)
}

Here “requestWhenInUseAuthorization” means Requests permission to use location services while the app is in the foreground.

Step 6 − Run the application to get the latitude and longitude, Find the Complete code

import UIKit
import CoreLocation
class ViewController: UIViewController {
var locationManager = CLLocationManager()
}