iOS Core Location

iOS Location & Heading Related Properties

iOS 2-D Location Data Structure: CLLocation

iOS 2-Dimension location

@property (readonly) CLLocationCoordinate2D coordinate;
typedef {
    CLLocationDegrees latitude;
    CLLocationDegrees longitude;
} CLLocationCoordinate2D;
  • Negative means the data is invalid

iOS location altitude in meter

@property(readonly)CLLocationDistance altitude;
  • Negative means below sea level

iOS location accuracy in meter

@property (readonly) CLLocationAccuracy horizontalAccuracy;
@property (readonly) CLLocationAccuracy verticalAccuracy;

Can be compare with the following pre-defined constants

kCLLocationAccuracyBestForNavigation;
kCLLocationAccuracyBest;
kCLLocationAccuracyNearestTenMeters;
kCLLocationAccuracyHundredMeters;
kCLLocationAccuracyKilometer;
kCLLocationAccuracyThreeKilometers;
  • Higher accuracy may require GPS which drain a lot of battery power

Current Speed (meter/second)

@property (readonly) CLLocationSpeed speed;

Direction/Heading (0 degree for North)

@property (readonly) CLLocationDirection course;

Timestamp

@property (readonly) NSDate *timestamp;

iOS Direction/heading data structure: CLHeading

@property (readonly) CLLocationDirection magneticHeading;
@property (readonly) CLLocationDirection trueHeading;
  • Magnetic heading mean the magnetic north (in degree)
  • True heading mean the geographic North Pole
  • Negative means invalid

Direction/heading accuracy in degree

@property(readonly)CLLocationDirection headingAccuracy;

Timestamp

@property (readonly) NSDate *timestamp;

Core Location Configuration Setup

Kind of location monitor

Continual updates Continuos location update
Updates when significant changes in location  
Region-based updates User had enter certain region
Heading monitoring Direction changes
  • High accuracy monitoring drains power from the device quick

Verify hardware settings/capability

 +(BOOL)locationServicesEnabled; 
Is location monitoring enabled by user
 + (BOOL)headingAvailable; 
Can device provide direction information
 +(BOOL)significantLocationChangeMonitoringAvailable;
Check if continuous location change is available. Only for phone device
 +(BOOL)regionMonitoringAvailable;
For iOS 4 or above
 +(BOOL)regionMonitoringEnabled;
Whether user enable region monitor

A descriptive string when asking user for permission to use location feature

@property (copy) NSString *purpose;

iOS Location Monitoring

iOS Location Monitoring Configuration

Configure the frequency of location updates

  • By desired accuracy
    @property CLLocationAccuracy desiredAccuracy;
    
  • By certain distance change in location
    @property CLLocationDistance distanceFilter;
    

iOS Location Monitoring Programming

Start/stop

- (void)startUpdatingLocation;
- (void)stopUpdatingLocation;
  • Stop monitor as fast as possible to stop power drain

Handling location updates in your application (CLLocationManage delegate)

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;

???

Location updates when significant location changes

- (void)startMonitoringSignificantLocationChanges;
- (void)stopMonitoringSignificantLocationChanges;
  • If the application is running, use the same delegate above to listen to location changes
  • If the application is not running or in background
  • iOS calls application:didFinishLaunchingWithOptions:
    • dictionary will contain UIApplicationLaunchOptionsLocationKey

Location updates when region changes

- (void)startMonitoringForRegion:(CLRegion *) desiredAccuracy:(CLLocationAccuracy);
- (void)stopMonitoringForRegion:(CLRegion *);

Delegate in listening region changes

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region;
??   - (void)locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
                 withError:(NSError *)error;
  • When application is not running, iOS calls application:didFinishLaunchingWithOptions:

Delegate in handling error like user permission denied

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error;
  • kCLErrorLocationUnknown: Likely temporarily not available
  • kCLErrorDenied: User denny application request
  • kCLErrorHeadingFailure: failure because of magnetic interference

iOS Heading Monitoring

iOS Heading Monitoring Configuration

Configure the frequency of heading updates

@property CLLocationDegrees headingFilter;
  • The min degree of heading changes before firing an update

Define what is 0 degree for your heading

@property CLHeadingOrientation headingOrientation;

iOS Heading Monitoring Programming

- (void)startUpdatingHeading;
- (void)stopUpdatingHeading;

Heading location updates in your application (CLLocationManage delegate)

- (void)locationManager:(CLLocationManager *)manager
       didUpdateHeading:(CLHeading *)newHeading;

To dismiss the automatic heading calibration UI

  • return NO
    - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager;
    
  • Or
    - (void)dismissHeadingCalibrationDisplay;
    

Other iOS Core Location API

Distance between 2 locations

- (CLLocationDistance)distanceFromLocation:(CLLocation *)otherLocation; // in meters