- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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() override func viewDidLoad() { super.viewDidLoad() locationManager.requestWhenInUseAuthorization() var currentLoc: CLLocation! if(CLLocationManager.authorizationStatus() == .authorizedWhenInUse || CLLocationManager.authorizationStatus() == .authorizedAlways) { currentLoc = locationManager.location print(currentLoc.coordinate.latitude) print(currentLoc.coordinate.longitude) } } }
- Related Articles
- How to get current location latitude and longitude in Android?
- How to track the current location (Latitude and Longitude) in an android device using Kotlin?\n
- How to get the longitude and latitude of a city using Python?
- How to get a complete address from latitude and longitude on Android Kotlin?
- How to find the latitude and longitude from address in Android?
- How to complete address from latitude and longitude on Android?
- How to use HTML5 Geolocation Latitude/Longitude API?
- Latitude and longitude of the user's position in JavaScript?
- How do I get an address latitude-longitude using HTML5 Geolocation or Google API?
- How to get current date and time from internet in iOS?
- How to get current location in android web view?
- How to get current location provider information in android?
- How can I find the latitude and longitude from address on Android using Kotlin?
- How to get current GPS location programmatically on Android?
- How to get the current version of my iOS project in code?
