LocationService

class LocationService(val context: Context)

Service to obtain device location.

Constructors

Link copied to clipboard
constructor(context: Context)

Types

Link copied to clipboard
object Companion
Link copied to clipboard

Google Play Services status.

Link copied to clipboard

Listener to notify current location. When current location is requested using getCurrentLocation, method returns immediately and listener might be called later if no cached location is readily available, in such cases system must wait until a new location update is received.

Link copied to clipboard

Listener to notify periodic location updates.

Properties

Link copied to clipboard

context where this service is instantiated from.

Link copied to clipboard

Indicates whether Google Play Services is available. More detailed information can be obtained at googlePlayServicesStatus.

Link copied to clipboard

Gets status for Google Play Services.

Link copied to clipboard

Indicates whether location is enabled on current device. True indicates that location is enabled, false indicates that location is disabled, null indicates that status is unknown. Notice that location status is only available for SDK 28 or later.

Link copied to clipboard

Listener to be notified of location updates.

Link copied to clipboard

Minimum distance between location updates in meters. By default this is zero meters. By modifying this value, the frequency to request location updates changes, and consequently so does battery usage. A reasonable value must be used for each use case. Changing this value while updates are already being requested has no effect until location updates are cancelled by calling cancelLocationUpdates and new updates are requested again.

Link copied to clipboard

Minimum time interval between location updates in milliseconds. By default this is one second. By modifying this value, the frequency to request location updates changes, and consequently so does battery usage. A reasonable value must be used for each use case. Changing this value while updates are already being requested has no effect until location updates are cancelled by calling cancelLocationUpdates and new updates are requested again.

Functions

Link copied to clipboard

Cancels current location request if there is an ongoing request, otherwise makes no action.

Link copied to clipboard

Cancels periodic location updates. This should be called whenever periodic location updates are no longer needed to preserve battery life.

Link copied to clipboard
@RequiresPermission(anyOf = ["android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"])
fun getCurrentLocation(listener: LocationService.OnCurrentLocationListener)

Requests a single location update. Returns a cached last known location if it was recently cached or waits until a new location is received if none is already available. Because this method might use cached locations, returned value in callback may not be the most recent one. This method returns immediately and notifies in provided listener when location is found. Current location request can be cancelled by calling cancelCurrentLocation. Once the request is cancelled, provider listener is no longer called. There are no guarantees on which thread the listener will be called on. This will depend on Android version on the device and whether Google Play Services location provider is available or not.

Link copied to clipboard
@RequiresPermission(anyOf = ["android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"])
fun getLastKnownLocation(): Location?

Gets last known location. This method attempts to retrieve last cached device location, if available. If no previous location is known, null is returned. This method does not attempt to retrieve a new location if no location is known. Location permission is required

Link copied to clipboard
@RequiresPermission(anyOf = ["android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"])
fun requestLocationUpdates()

Requests continuous location updates for current updateInterval and smallestDisplacement. By modifying both updateInterval and smallestDisplacement, the frequency to request location updates changes, and consequently so does battery usage. Reasonable values must be used for each use case. Current locationUpdateListener will be notified until cancelLocationUpdates is called.