Tagged:

There are number of techniques available in iOS SDK to obtain information about the current geographical location of iOS device. It includes GPS, cell tower triangulation and IP address of available internet connection and the device will automatically detect the most accurate solution available. In this tech resource we will learn how to use Core Location framework in iPhone application to detect the device’s current location.

Core Location Basics

There are two key classes in CoreLocation framework named CLLocationManager and CLLoccation. To create instance of these classes first we have to include the CoreLocation framework in the Xcode project. Below is the code to get current location by creating an instance of CLLocationManager class within the application.

Let’s see how this code will work to get current location along with some other information in details.

Set desired Location Accuracy

desiredAccuracy property of the CLLocationManager object is used to specify the level of accuracy of location. Keep in mind that greater the level of accuracy selected, greater the drainage of device battery.

Below are the number of predefined constant variables available to use while configuring this property.

  • kCLLocationAccuracyBestForNavigation (highest possible level of accuracy)
  • kCLLocationAccuracyBest (highest recommended level of accuracy)
  • kCLLocationAccuracyNearestTenMeters (Accurate to within 10 meters)
  • kCLLocationAccuracyHundredMeters (Accurate to within 100 meters)
  • kCLLocationAccuracyKilometer (Accurate to within one kilometre)
  • kCLLocationAccuracyThreeKilometers (Accurate to within three kilometres)

Configure Distance Filter

As a default configuration, location manager will report updates whenever any change will be detected in the device location. You can configure it manually if you don’t want to send location update frequently with minor change in location. You can set distanceFilter property of the location manager which specifies the amount of distance that must be changed before an update is triggered.

In the example given above the distance filter is set to 2000 meters. So the application will receive an update in location only if device will travel 2000 meters from the location of the last update.