Distance Filter using
E.g. In the above example code above, location changes of less than 5 metres won’t be sent to the callback, but instead be ignored.
Get User Location Using CLLocationManager
1 - Include the CoreLocation.framework in your project; this is accomplished by clicking on:
Click on the (+) button, look for CoreLocation.framework and click add.
2- Modify the info.plist file to ask for permission to use user location by opening it as source code. Add either of the following key:value pair under the tag to ask for usage of user's location while the application is in use:
3- import CoreLocation to the ViewController that will be using it.
4- Make sure your ViewController conforms to the CLLocationManagerDelagate protocol
After these steps, we can create a CLLocationManager object as instance variable and use it in the ViewController.
We do not use 'let' here because we will modify the manager to specify its delegate, minimum distance before update event, and its accuracy
Now to get access to the location updates, we can implement the function below which is called overtime the distanceFilter is reached.
The locations parameter is an array of CLLocation objects that represent the actual location of the device.From these objects, one can get access to the following attributes:
coordinate,altitude, floor, horizontalAccuracy, verticalAccuracy, timestamp, description, course, speed, and a function
distance(from:) that measures the distance between two locations.
Note: While requesting permission for location, there are two different types of authorization.
"When In Use" authorization only gives the app permission to receive your location when the app is in use or foreground.
“Always" authorization, gives the app background permissions which may lead to decrease battery life in case your app is closed.
Plist file should be adjusted as necessary.