iPhone OS Reference Library Apple Developer Connection spyglass button

CLLocationManager Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/CoreLocation.framework
Availability
Available in iPhone OS 2.0 and later.
Declared in
CLError.h
CLErrorDomain.h
CLHeading.h
CLLocation.h
CLLocationManager.h
Related sample code

Overview

The CLLocationManager class defines the interface for configuring the delivery of location- and heading-related events to your application. You use an instance of this class to establish the parameters that determine when location and heading events should be delivered. You can also use a location manager object to retrieve the most recent location data.

To use a CLLocationManager object to deliver location events, create an instance, assign a delegate object to it, configure the desired accuracy and distance filter values, and call the startUpdatingLocation method. The location service returns an initial location as quickly as possible, returning cached information when available. After delivery of the initial event notification, the CLLocationManager object may deliver additional events if the minimum threshold distance (as specified by the distanceFilter property) is exceeded or a more accurate location value is determined.

Important: The user has the option of denying an application’s access to the location service data. During its initial uses by an application, the Core Location framework prompts the user to confirm that using the location service is acceptable. If the user denies the request, the CLLocationManager object reports an appropriate error to its delegate during future requests.

In iPhone OS, a device with the appropriate hardware may also report heading information. When the value in the headingAvailable property is YES, you can use a location manager object to retrieve heading information. To begin the delivery of heading-related events, assign a delegate to the location manager object and call the location manager’s startUpdatingHeading method. If location updates are also enabled, the location manager returns both the true heading and magnetic heading values. If location updates are not enabled, the location manager returns only the magnetic heading value. These features are not available in Mac OS X.

The delegate you assign to the CLLocationManager object must conform to the CLLocationManagerDelegate protocol. For information about the methods of this protocol, see CLLocationManagerDelegate Protocol Reference.

Tasks

Configuring Location Updates

Configuring Heading Updates

Starting and Stopping Updates

Getting the Location Data

Properties

For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.

delegate

The delegate object you want to receive update events.

@property(assign, NS_NONATOMIC_IPHONEONLY) id<CLLocationManagerDelegate> delegate
Special Considerations

In iPhone OS, this property is declared as nonatomic. In Mac OS X, it is declared as atomic.

Availability
Related Sample Code
Declared In
CLLocationManager.h

desiredAccuracy

The desired accuracy of the location data.

@property(assign, NS_NONATOMIC_IPHONEONLY) CLLocationAccuracy desiredAccuracy
Discussion

The receiver does its best to achieve the requested accuracy; however, the actual accuracy is not guaranteed.

You should assign a value to this property that is appropriate for your usage scenario. In other words, if you need only the current location within a few kilometers, you should not specify kCLLocationAccuracyBest for the accuracy. Determining a location with greater accuracy requires more time and more power.

When requesting high accuracy location data, the initial event delivered by the location service may not have the accuracy you requested. The location service delivers the initial event as quickly as possible. It then continues to determine the location with the accuracy you requested and delivers additional events, as necessary, when that data is available.

The default value of this property is kCLLocationAccuracyBest.

Special Considerations

In iPhone OS, this property is declared as nonatomic. In Mac OS X, it is declared as atomic.

Availability
Related Sample Code
Declared In
CLLocationManager.h

distanceFilter

The minimum distance (measured in meters) a device must move laterally before an update event is generated.

@property(assign, NS_NONATOMIC_IPHONEONLY) CLLocationDistance distanceFilter
Discussion

This distance is measured relative to the previously delivered location. Use the value kCLDistanceFilterNone to be notified of all movements.

The default value of this property is kCLDistanceFilterNone.

Special Considerations

In iPhone OS, this property is declared as nonatomic. In Mac OS X, it is declared as atomic.

Availability
Related Sample Code
Declared In
CLLocationManager.h

headingAvailable

A Boolean value indicating whether the location manager is able to generate heading-related events.

@property(readonly, nonatomic) BOOL headingAvailable
Discussion

Heading data may not be available on all iPhone OS–based devices. You should check the value of this property before asking the location manager to deliver heading-related events.

Availability
See Also
Related Sample Code
Declared In
CLLocationManager.h

headingFilter

The minimum angular change (measured in degrees) required to generate new heading events.

@property(assign, nonatomic) CLLocationDegrees headingFilter
Discussion

The angular distance is measured relative to the last delivered heading event. Use the value kCLHeadingFilterNone to be notified of all movements.

The default value of this property is kCLHeadingFilterNone.

Availability
Related Sample Code
Declared In
CLLocationManager.h

location

The most recently retrieved user location. (read-only)

@property(readonly, NS_NONATOMIC_IPHONEONLY) CLLocation *location
Discussion

The value of this property is nil if no location data has ever been retrieved.

It is a good idea to check the timestamp of the location that is returned. If the receiver is currently gathering location data, but the minimum distance filter is large, the returned location might be relatively old. If it is, you can stop the receiver and start it again to force an update.

Special Considerations

In iPhone OS, this property is declared as nonatomic. In Mac OS X, it is declared as atomic.

Availability
See Also
Related Sample Code
Declared In
CLLocationManager.h

locationServicesEnabled

A Boolean value indicating whether location services are enabled on the device.

@property(readonly, NS_NONATOMIC_IPHONEONLY) BOOL locationServicesEnabled
Discussion

The user can enable or disable location services altogether from the Settings application by toggling the switch in Settings > General > Location Services.

You should check this property before starting location updates to determine if the user has location services enabled for the current device. If this property contains the value NO and you start location updates anyway, the Core Location framework prompts the user with a confirmation panel asking whether location services should be reenabled.

Special Considerations

In iPhone OS, this property is declared as nonatomic. In Mac OS X, it is declared as atomic.

Availability
See Also
Related Sample Code
Declared In
CLLocationManager.h

Instance Methods

dismissHeadingCalibrationDisplay

Dismisses the heading calibration view from the screen immediately.

- (void)dismissHeadingCalibrationDisplay

Discussion

Core Location uses the heading calibration panel to calibrate the available heading hardware as needed. The display of this view is automatic, assuming your delegate supports displaying the view at all. If the view is displayed, you can use this method to dismiss it after an appropriate amount of time to ensure that your application’s user interface is not unduly disrupted.

Availability
  • Available in iPhone OS 3.0 and later.
Declared In
CLLocationManager.h

startUpdatingHeading

Starts the generation of updates that report the user’s current heading.

- (void)startUpdatingHeading

Discussion

This method returns immediately. Calling this method when the receiver is stopped causes it to obtain an initial heading and notify your delegate. After that, the receiver generates update events when the value in the headingFilter property is exceeded.

Before calling this method, you should always check the headingAvailable property to see if heading information is supported on the current device. If heading information is not supported, calling this method has no effect and does not result in the delivery of events to your delegate.

Calling this method several times in succession does not automatically result in new events being generated. Calling stopUpdatingHeading in between, however, does cause a new initial event to be sent the next time you call this method.

Availability
  • Available in iPhone OS 3.0 and later.
See Also
Declared In
CLLocationManager.h

startUpdatingLocation

Starts the generation of updates that report the user’s current location.

- (void)startUpdatingLocation

Discussion

This method returns immediately. Calling this method when the receiver is stopped causes it to obtain an initial location fix (which may take several seconds) and notify your delegate. After that, the receiver generates update events primarily when the value in the distanceFilter property is exceeded. Updates may be delivered in other situations though. For example, the receiver may send another notification if the hardware gathers a more accurate location reading.

Calling this method several times in succession does not automatically result in new events being generated. Calling stopUpdatingLocation in between, however, does cause a new initial event to be sent the next time you call this method.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Related Sample Code
Declared In
CLLocationManager.h

stopUpdatingHeading

Stops the generation of heading updates.

- (void)stopUpdatingHeading

Discussion

You should call this method whenever your code no longer needs to receive heading-related events. Disabling event delivery gives the receiver the option of disabling the appropriate hardware (and thereby saving power) when no clients need location data. You can always restart the generation of heading updates by calling the startUpdatingHeading method again.

Availability
  • Available in iPhone OS 3.0 and later.
See Also
Related Sample Code
Declared In
CLLocationManager.h

stopUpdatingLocation

Stops the generation of location updates.

- (void)stopUpdatingLocation

Discussion

You should call this method whenever your code no longer needs to receive location-related events. Disabling event delivery gives the receiver the option of disabling the appropriate hardware (and thereby saving power) when no clients need location data. You can always restart the generation of location updates by calling the startUpdatingLocation method again.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Related Sample Code
Declared In
CLLocationManager.h

Constants

CLLocationDistance

A distance measurement (in meters) from an existing location.

typedef double CLLocationDistance;
Availability
  • Available in iPhone OS 2.0 and later.
Declared In
CLLocation.h

Distance Filter Value

This constant indicates the minimum distance required before an event is generated.

extern const CLLocationDistance kCLDistanceFilterNone;
Constants
kCLDistanceFilterNone

All movements are reported.

Available in iPhone OS 2.0 and later.

Declared in CLLocation.h.

Heading Filter Value

This constant indicates the minimum heading change before an event is generated.

const CLLocationDegrees kCLHeadingFilterNone;
Constants
kCLHeadingFilterNone

All heading changes are reported.

Available in iPhone OS 3.0 and later.

Declared in CLHeading.h.

CLError

Error codes returned by the location manager object.

typedef enum {
   kCLErrorLocationUnknown  = 0,
   kCLErrorDenied,
   kCLErrorNetwork,
   kCLErrorHeadingFailure
} CLError;
Constants
kCLErrorLocationUnknown

The location manager was unable to obtain a location value right now.

Available in iPhone OS 2.0 and later.

Declared in CLError.h.

kCLErrorDenied

Access to the location service was denied by the user.

Available in iPhone OS 2.0 and later.

Declared in CLError.h.

kCLErrorNetwork

The network was unavailable or a network error occurred.

Available in iPhone OS 3.0 and later.

Declared in CLError.h.

kCLErrorHeadingFailure

The heading could not be determined.

Available in iPhone OS 3.0 and later.

Declared in CLError.h.

Discussion

Errors are delivered to the delegate using an NSError object.

kCLErrorDomain

The domain for Core Location errors.

extern NSString *const kCLErrorDomain;
Constants
kCLErrorDomain

The domain for Core Location errors. This value is used in the NSError class.

Available in iPhone OS 2.0 and later.

Declared in CLErrorDomain.h.



Last updated: 2009-08-04

Did this document help you? Yes It's good, but... Not helpful...