CLLocationManager Class

Definition

Manages the delivery of location, region, and heading events to your application.

[Foundation.Register("CLLocationManager", true)]
public class CLLocationManager : Foundation.NSObject
[<Foundation.Register("CLLocationManager", true)>]
type CLLocationManager = class
    inherit NSObject
Inheritance
CLLocationManager
Attributes

Remarks

Requesting Authorization

Developers that want to request access to the location information must request permission from the user to do so before they can receive events. This is done by calling either the RequestWhenInUseAuthorization() method. When these methods are invoked, the system will prompt the user for authorization, and if he grants it, the AuthorizationChanged event will be raised if set (or if you are using the Delegate idiom, the AuthorizationChanged(CLLocationManager, CLAuthorizationStatus) method will be invoked.

Additionally, developers must add one or both of the keys NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription in their app's info.plist. These keys are strings that can be used to describe why the app needs location access.

Developers should use an idiom like this:

var manager = new CLLocationManager();
manager.AuthorizationChanged += (sender, args) => {
  Console.WriteLine ("Authorization changed to: {0}", args.Status);
};
if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
    manager.RequestWhenInUseAuthorization();

Tracking the device's location

The most common use-case for the CLLocationManager is tracking the device while the application is in the foreground. (See also "Background Updating and Deferred Mode" below.)

Application developers may use either C#-style events or Apple's delegate-object pattern to track foreground location updating. For C#-style events, developers can use the LocationsUpdated event:

var mgr = new CLLocationManager();
mgr.LocationsUpdated += (sender, e) => {
  foreach(var loc in e.Locations)
  {
    Console.WriteLine(loc);
  }
};
mgr.StartUpdatingLocation();
let mgr = new CLLocationManager()
mgr.LocationsUpdated.Add( fun e ->
    e.Locations |> Seq.map Console.WriteLine |> ignore )
mgr.StartUpdatingLocations()          

While C#-style events are more concise, the CLLocationManager must use the delegate-object pattern for certain behaviors (for instance, deferred updating), and it may be more consistent for an application to use the delegate-object pattern even when C#-style events are available. The delegate-object pattern consists of assigning a customized CLLocationManagerDelegate object to the Delegate property of the CLLocationManager:

var mgr = new CLLocationManager();
mgr.Delegate = new MyLocationDelegate();
mgr.StartUpdatingLocation();
//...etc...

public class MyLocationDelegate : CLLocationManagerDelegate {
  public override void LocationsUpdated (CLLocationManager manager, CLLocation[] locations) {
    foreach(var loc in locations) {
      Console.WriteLine(loc);
    }
  }
}          
let mgr = new CLLocationManager()
mgr.Delegate <- new MyLocationDelegate()
mgr.StartUpdatingLocation()
//...etc...

type MyLocationDelegate () = inherit CLLocationManagerDelegate()
    override this.LocationsUpdated ( manager : CLLocationManager, locations : CLLocation[] ) = 
        locations 
        |> Seq.map Console.WriteLine
        |> ignore          
<block subset="none" type="behaviors">

Background Updating and Deferred Mode

Locations can be updated while the application is in the background. This requires that the info.plist be modified using either Xamarin Studio's visual editor:

Or by adding the key manually:

<example>

<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>            

</example> </block> <block subset="none" type="behaviors">

Importance of Delegate object

Generally, when using Xamarin.iOS, developers can freely choose whether to use C# events or Apple-style "delegate objects" to react to object lifecycle events. Several CLLocationManager methods, however, require the delegate-object pattern.

</block>

Constructors

Name Description
CLLocationManager()

Creates a new CLLocationManager with default values.

CLLocationManager(NativeHandle)

A constructor used when creating managed representations of unmanaged objects. Called by the runtime.

CLLocationManager(NSObjectFlag)

Constructor to call on derived classes to skip initialization and merely allocate the object.

Properties

Name Description
AccessibilityAttributedUserInputLabels (Inherited from NSObject)
AccessibilityRespondsToUserInteraction (Inherited from NSObject)
AccessibilityTextualContext (Inherited from NSObject)
AccessibilityUserInputLabels (Inherited from NSObject)
AccuracyAuthorization
ActivityType

Used to provide the operating system clues for better power consumption / accuracy.

AllowsBackgroundLocationUpdates

Gets or sets a Boolean value that controls whether the application will respond to location updates while it is suspended.

AuthorizationStatus
Class (Inherited from NSObject)
ClassHandle

The Objective-C class handle for this class.

DebugDescription (Inherited from NSObject)
DeferredLocationUpdatesAvailable

Whether background-generated deferred location data are available.

Delegate

An instance of the CoreLocation.ICLLocationManagerDelegate model class which acts as the class delegate.

Description (Inherited from NSObject)
DesiredAccuracy

The accuracy preferred by the app. (Coarser accuracies consume less power.)

DistanceFilter

The minimum horizontal distance, in meters, the device has to move before issuing a location update.

ExposedBindings (Inherited from NSObject)
Handle

Handle (pointer) to the unmanaged object representation.

(Inherited from NSObject)
Heading

The most recent heading (direction in which the device is traveling).

HeadingAvailable

Whether the Heading property is not null.

HeadingFilter

The minimum change in heading, in degreees, necessary to generate a location update.

HeadingOrientation

The orientation used to determine heading calculations.

IsAuthorizedForWidgetUpdates
IsDirectBinding (Inherited from NSObject)
IsProxy (Inherited from NSObject)
IsRangingAvailable

Gets a Boolean value that tells whether the device can range Bluetooth beacons.

Location

The most recently-retrieved CLLocation.

LocationServicesEnabled

Whether location services are available.

MaximumRegionMonitoringDistance

The largest boundary distance, in meters, that can be assigned to a region.

MaxTimeInterval

Represents the value associated with the constant CLTimeInternalMax

MonitoredRegions

The set of CLRegions being monitored by the app.

PausesLocationUpdatesAutomatically

Whether the system is allowed to pause location updates (for instance, if the device has not moved in awhile).

Purpose

Developers should not use this deprecated property.

RangedBeaconConstraints
RangedRegions

The set of CLRegions being tracked using ranging.

RegionMonitoringAvailable

Application developers should use IsMonitoringAvailable(Type) rather than this deprecated method.

RegionMonitoringEnabled

Application developers should use IsMonitoringAvailable(Type) rather than this deprecated method.

RetainCount (Inherited from NSObject)
Self (Inherited from NSObject)
ShouldDisplayHeadingCalibration
ShowsBackgroundLocationIndicator
SignificantLocationChangeMonitoringAvailable

Whether "significant location change" monitoring (e.g., via cell tower switch) is available.

Status

The authorization status of the app (e.g., if the app is denied access to location services).

Superclass (Inherited from NSObject)
SuperHandle

Handle used to represent the methods in the base class for this NSObject.

(Inherited from NSObject)
WeakDelegate

An object that can respond to the delegate protocol for this type

Zone (Inherited from NSObject)

Methods

Name Description
AddObserver(NSObject, NSString, NSKeyValueObservingOptions, IntPtr) (Inherited from NSObject)
AddObserver(NSObject, String, NSKeyValueObservingOptions, IntPtr) (Inherited from NSObject)
AddObserver(NSString, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AddObserver(String, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AllowDeferredLocationUpdatesUntil(Double, Double)

Suggests that location updates are deferred until either distance has been traveled or timeout has passed.

AwakeFromNib() (Inherited from NSObject)
BeginInvokeOnMainThread(Action) (Inherited from NSObject)
BeginInvokeOnMainThread(Selector, NSObject)

Invokes asynchrously the specified code on the main UI thread.

(Inherited from NSObject)
Bind(NSString, NSObject, String, NSBindingOptions) (Inherited from NSObject)
Bind(NSString, NSObject, String, NSDictionary) (Inherited from NSObject)
CommitEditing() (Inherited from NSObject)
CommitEditing(NSObject, Selector, IntPtr) (Inherited from NSObject)
ConformsToProtocol(NativeHandle) (Inherited from NSObject)
Copy() (Inherited from NSObject)
DangerousAutorelease()

Calls the 'autorelease' selector on this object.

(Inherited from NSObject)
DangerousRelease()

Calls the 'release' selector on this object.

(Inherited from NSObject)
DangerousRetain()

Calls the 'retain' selector on this object.

(Inherited from NSObject)
DidChange(NSKeyValueChange, NSIndexSet, NSString) (Inherited from NSObject)
DidChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
DidChangeValue(String) (Inherited from NSObject)
DisallowDeferredLocationUpdates()

Turns off deferred background location updates.

DismissHeadingCalibrationDisplay()

Removes the heading calibration view from the display.

Dispose()

Releases the resources used by the NSObject object.

(Inherited from NSObject)
Dispose(Boolean)
DoesNotRecognizeSelector(Selector) (Inherited from NSObject)
Equals(NSObject) (Inherited from NSObject)
Equals(Object) (Inherited from NSObject)
GetBindingInfo(NSString) (Inherited from NSObject)
GetBindingOptionDescriptions(NSString) (Inherited from NSObject)
GetBindingValueClass(NSString) (Inherited from NSObject)
GetDictionaryOfValuesFromKeys(NSString[]) (Inherited from NSObject)
GetHashCode()

Generates a hash code for the current instance.

(Inherited from NSObject)
GetMethodForSelector(Selector) (Inherited from NSObject)
GetNativeHash() (Inherited from NSObject)
Init() (Inherited from NSObject)
InitializeHandle(NativeHandle, String) (Inherited from NSObject)
InitializeHandle(NativeHandle) (Inherited from NSObject)
Invoke(Action, Double) (Inherited from NSObject)
Invoke(Action, TimeSpan) (Inherited from NSObject)
InvokeOnMainThread(Action) (Inherited from NSObject)
InvokeOnMainThread(Selector, NSObject)

Invokes synchrously the specified code on the main UI thread.

(Inherited from NSObject)
IsEqual(NSObject) (Inherited from NSObject)
IsKindOfClass(Class) (Inherited from NSObject)
IsMemberOfClass(Class) (Inherited from NSObject)
IsMonitoringAvailable(Class)

Determines whether the device supports region monitoring for the specified kind of CLRegion.

IsMonitoringAvailable(Type)

Determines whether the device supports region monitoring for the specified kind of CLRegion.

MarkDirty()

Promotes a regular peer object (IsDirectBinding is true) into a toggleref object.

(Inherited from NSObject)
MutableCopy() (Inherited from NSObject)
ObjectDidEndEditing(NSObject) (Inherited from NSObject)
ObserveValue(NSString, NSObject, NSDictionary, IntPtr) (Inherited from NSObject)
PerformSelector(Selector, NSObject, Double, NSString[]) (Inherited from NSObject)
PerformSelector(Selector, NSObject, Double) (Inherited from NSObject)
PerformSelector(Selector, NSObject, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean, NSString[]) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean) (Inherited from NSObject)
PerformSelector(Selector) (Inherited from NSObject)
PrepareForInterfaceBuilder() (Inherited from NSObject)
RemoveObserver(NSObject, NSString, IntPtr) (Inherited from NSObject)
RemoveObserver(NSObject, NSString) (Inherited from NSObject)
RemoveObserver(NSObject, String, IntPtr) (Inherited from NSObject)
RemoveObserver(NSObject, String) (Inherited from NSObject)
RequestAlwaysAuthorization()

Displays an interface to the user that requests authorization to use location services any time that the app is running.

RequestLocation()

Requests the current location.

RequestState(CLRegion)

Asynchronously requests information on the state of the region.

RequestTemporaryFullAccuracyAuthorization(String, Action<NSError>)
RequestTemporaryFullAccuracyAuthorization(String)
RequestTemporaryFullAccuracyAuthorizationAsync(String)
RequestWhenInUseAuthorization()

Displays an interface to the user that requests authorization to use location services any time that the app is in the foreground.

RespondsToSelector(Selector) (Inherited from NSObject)
SetNilValueForKey(NSString) (Inherited from NSObject)
SetValueForKey(NSObject, NSString) (Inherited from NSObject)
SetValueForKeyPath(NativeHandle, NSString) (Inherited from NSObject)
SetValueForKeyPath(NSObject, NSString) (Inherited from NSObject)
SetValueForUndefinedKey(NSObject, NSString) (Inherited from NSObject)
SetValuesForKeysWithDictionary(NSDictionary) (Inherited from NSObject)
StartMonitoring(CLRegion, Double)

Starts monitoring the region.

StartMonitoring(CLRegion)

Begins monitoring region for entry and exit.

StartMonitoringLocationPushes(Action<NSData,NSError>)
StartMonitoringLocationPushesAsync()
StartMonitoringSignificantLocationChanges()

Starts monitoring for significant changes.

StartMonitoringVisits()

Starts generating events in response to visits.

StartRangingBeacons(CLBeaconIdentityConstraint)
StartRangingBeacons(CLBeaconRegion)

Starts delivering notifications about beacons in region.

StartUpdatingHeading()

Starts updating the heading.

StartUpdatingLocation()

Starts updating the location

StopMonitoring(CLRegion)

Stops monitoring the region.

StopMonitoringLocationPushes()
StopMonitoringSignificantLocationChanges()

Starts monitoring significant location changes.

StopMonitoringVisits()

Stops generating events in response to visits.

StopRangingBeacons(CLBeaconIdentityConstraint)
StopRangingBeacons(CLBeaconRegion)

Stops tracking beacons in the region.

StopUpdatingHeading()

Stops updating the heading.

StopUpdatingLocation()

Stops updating the location.

ToString()

Returns a string representation of the value of the current instance.

(Inherited from NSObject)
Unbind(NSString) (Inherited from NSObject)
ValueForKey(NSString) (Inherited from NSObject)
ValueForKeyPath(NSString) (Inherited from NSObject)
ValueForUndefinedKey(NSString) (Inherited from NSObject)
WillChange(NSKeyValueChange, NSIndexSet, NSString) (Inherited from NSObject)
WillChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
WillChangeValue(String) (Inherited from NSObject)

Events

Name Description
AuthorizationChanged
DeferredUpdatesFinished
DidChangeAuthorization
DidDetermineState
DidFailRangingBeacons
DidRangeBeacons
DidRangeBeaconsSatisfyingConstraint
DidStartMonitoringForRegion
DidVisit
Failed
LocationsUpdated
LocationUpdatesPaused
LocationUpdatesResumed
MonitoringFailed
RangingBeaconsDidFailForRegion
RegionEntered
RegionLeft
UpdatedHeading
UpdatedLocation

Extension Methods

Name Description
ObjectDidBeginEditing(NSObject, INSEditor)
ObjectDidEndEditing(NSObject, INSEditor)
GetValidModes(NSObject, NSFontPanel)
ValidateToolbarItem(NSObject, NSToolbarItem)
BrowserAccessibilityDeleteTextAtCursor(NSObject, IntPtr)
BrowserAccessibilityInsertTextAtCursor(NSObject, String)
GetAccessibilityLineEndPositionFromCurrentSelection(NSObject)
GetAccessibilityLineRangeForPosition(NSObject, IntPtr)
GetAccessibilityLineStartPositionFromCurrentSelection(NSObject)
GetBrowserAccessibilityAttributedValue(NSObject, NSRange)
GetBrowserAccessibilityContainerType(NSObject)
GetBrowserAccessibilityCurrentStatus(NSObject)
GetBrowserAccessibilityHasDomFocus(NSObject)
GetBrowserAccessibilityIsRequired(NSObject)
GetBrowserAccessibilityPressedState(NSObject)
GetBrowserAccessibilityRoleDescription(NSObject)
GetBrowserAccessibilitySelectedTextRange(NSObject)
GetBrowserAccessibilitySortDirection(NSObject)
GetBrowserAccessibilityValue(NSObject, NSRange)
SetBrowserAccessibilityContainerType(NSObject, BEAccessibilityContainerType)
SetBrowserAccessibilityCurrentStatus(NSObject, String)
SetBrowserAccessibilityHasDomFocus(NSObject, Boolean)
SetBrowserAccessibilityIsRequired(NSObject, Boolean)
SetBrowserAccessibilityPressedState(NSObject, BEAccessibilityPressedState)
SetBrowserAccessibilityRoleDescription(NSObject, String)
SetBrowserAccessibilitySelectedTextRange(NSObject, NSRange)
SetBrowserAccessibilitySortDirection(NSObject, String)
ProvideImageData(NSObject, IntPtr, UIntPtr, UIntPtr, UIntPtr, UIntPtr, UIntPtr, NSObject)
ProvideImageToMTLTexture(NSObject, IMTLTexture, IMTLCommandBuffer, UIntPtr, UIntPtr, UIntPtr, UIntPtr, NSObject)
SetSharedObservers(NSObject, NSKeyValueSharedObserversSnapshot)
GetDebugDescription(INSObjectProtocol)
GetHandle(INativeObject)
GetNonNullHandle(INativeObject, String)
AcceptsPreviewPanelControl(NSObject, QLPreviewPanel)
BeginPreviewPanelControl(NSObject, QLPreviewPanel)
EndPreviewPanelControl(NSObject, QLPreviewPanel)
GetAccessibilityCustomRotors(NSObject)

Gets the array of UIAccessibilityCustomRotor objects appropriate for this object.

SetAccessibilityCustomRotors(NSObject, UIAccessibilityCustomRotor[])

Sets the array of UIAccessibilityCustomRotor objects appropriate for this object.

AccessibilityHitTest(NSObject, CGPoint, UIEvent)
GetAccessibilityNextTextNavigationElement(NSObject)
GetAccessibilityNextTextNavigationElementBlock(NSObject)
GetAccessibilityPreviousTextNavigationElement(NSObject)
GetAccessibilityPreviousTextNavigationElementBlock(NSObject)
SetAccessibilityNextTextNavigationElement(NSObject, NSObject)
SetAccessibilityNextTextNavigationElementBlock(NSObject, AXObjectReturnBlock)
SetAccessibilityPreviousTextNavigationElement(NSObject, NSObject)
SetAccessibilityPreviousTextNavigationElementBlock(NSObject, AXObjectReturnBlock)
GetAccessibilityTextInputResponder(NSObject)
GetAccessibilityTextInputResponderHandler(NSObject)
SetAccessibilityTextInputResponder(NSObject, IUITextInput)
SetAccessibilityTextInputResponderHandler(NSObject, UITextInputReturnHandler)

Applies to

See also