Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lohenyumnam/37c0fe43ed59d670d7e4e1a0ca74ab83 to your computer and use it in GitHub Desktop.
Save lohenyumnam/37c0fe43ed59d670d7e4e1a0ca74ab83 to your computer and use it in GitHub Desktop.
How to use CoreLocation in iOS using Swift 4 to get your location Speed, Floor, timestamp, Course Direction, Altitude above sea level
// How to use CoreLocation to get your location Speed, Floor, timestamp, Course Direction, Altitude above sea level
//TODO: Step 1 ####################################################################
// First of all import "CoreLocation"
import CoreLocation
//TODO: Step 2 ####################################################################
/*
Explain:
In order to use the instance of "CLLocationManager" class to get the location data such as coordinate, Speed etc in our own class through there pre define method,
we have to make our class a Delegate of "CLLocationManager"
So, to do that we have to conform our class to Protocol "CLLocationManagerDelegate" through which we will get access the method which allows us to access the required data,
Some of the method are:
1. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
2. func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
Note:
This method are optional which means we can either chooses to impliment or not but to use it we have to
Example:
say we want to get the location coordinate in our class "ViewController" eg: "WeatherViewController" we will conform as below
*/
class WeatherViewController: UIViewController, CLLocationManagerDelegate {
//TODO: Step 3 ####################################################################
// Now create an instance of a class "CLLocationManager"
let locationManager = CLLocationManager()
//TODO: Step 4 (Setup) ####################################################################
/*
Explain:
Time to tell the instance of "CLLocationManager" in our case "locationManager" (as we define in step 3)
that the class "WeatherViewController" is the delegate of "CLLocationManager" as we conform to Protocol "CLLocationManagerDelegate",
Note:
If we create our own custom Class the Setup step will be inside the init() Method but for our viewController viewDidLoad() is the method
*/
override func viewDidLoad() {
super.viewDidLoad()
//TODO:Set up the location manager here.
// Telling the instance of "CLLocationManager" in our case "locationManager" that the class "WeatherViewController" is the delegate of "CLLocationManager"
locationManager.delegate = self
/*
Explain:
In order to tell "locationManager" how Accurate we want our GPS location to be, we can setup using
1. kCLLocationAccuracyBestForNavigation
2. kCLLocationAccuracyBest
3. kCLLocationAccuracyNearestTenMeters
4. kCLLocationAccuracyHundredMeters
5. kCLLocationAccuracyKilometer
6. kCLLocationAccuracyThreeKilometers
Depending upon our requirment
*/
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
/*
Explain:
According to Apple's rule we need to ask for permission to use the Device sensor,
so for the we need to call a methode "requestAlwaysAuthorization()" or "requestWhenInUseAuthorization()" according to our need
Note:
To use this methode we need to first specified in Apps information proper list "info.plist" by adding the below XML code
<key>NSLocationWhenInUseUsageDescription</key>
<string> Description of why you need to use GPS location </string>
<key>NSLocationUsageDescription</key>
<string> Description of why you need to use GPS location </string>
*/
locationManager.requestWhenInUseAuthorization()
//TODO: Step 5 ####################################################################
// Its time to ask the "locationManager" to get the GPS coordinate using all the Setup we did in step 4
locationManager.startUpdatingLocation()
}
//TODO: Step 6 ####################################################################
// In step 5 "locationManager" staring collecting the GPS coordinate Data, now its time to access coordinate using didUpdateLocations method "func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) "
/*
Explain :
This method is already been Define by Apple in Protocol "CLLocationManagerDelegate" and the only way to get access to this method to get
the GPS data by our class is conforming to "CLLocationManagerDelegate" and call the methode
that's the reason why we did that in Step 2
*/
//Write the didUpdateLocations method here:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// "locations" contain the arrays of Data, in order to get the latest data we have to use the last one so,
// the last Data will be locations[locations.count - 1]
let location = locations[locations.count - 1]
// To know if the Data that our device is correct, The "horizontalAccuracy" value should be grater then ZERO for valid GPS coordinate Data
if location.horizontalAccuracy > 0 {
//TODO: Step 7 ####################################################################
// Time to stop collecting GPS data, as it will keep updating and will kill the battry of Device if we don't
locationManager.stopUpdatingLocation()
// Getting coordinate
let Longitude = location.coordinate.longitude
let latitude = location.coordinate.latitude
// Getting Speed
// The instantaneous speed of the device, measured in meters per second.
let speed = location.speed
// Getting no. of floor
// The logical floor of the building in which the user is located.
let floor = location.floor
// Getting The when the GPS data was taken
let timeStamp = location.timestamp
// The direction in which the device is traveling.
let courseDirection = location.course
// The altitude, measured in meters.
// Positive values indicate altitudes above sea level. Negative values indicate altitudes below sea level.
let hightFromSeaLevel = location.altitude
}
}
// when there is an Error while collecting GPS coordinate data by "locationManager" this method is called
//Write the didFailWithError method here:
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment