Google is committed to advancing racial equity for Black communities. See how.
Added in API level 1

Location

open class Location : Parcelable
kotlin.Any
   ↳ android.location.Location

A data class representing a geographic location.

A location can consist of a latitude, longitude, timestamp, and other information such as bearing, altitude and velocity.

All locations generated by the LocationManager are guaranteed to have a valid latitude, longitude, and timestamp (both UTC time and elapsed real-time since boot), all other parameters are optional.

Summary

Constants

static Int

Constant used to specify formatting of a latitude or longitude in the form "[+-]DDD.

static Int

Constant used to specify formatting of a latitude or longitude in the form "[+-]DDD:MM.

static Int

Constant used to specify formatting of a latitude or longitude in the form "DDD:MM:SS.

Inherited constants

Public constructors

<init>(provider: String!)

Construct a new Location with a named provider.

Construct a new Location object that is copied from an existing one.

Public methods

open Float

Returns the approximate initial bearing in degrees East of true North when traveling along the shortest path between this location and the given location.

open static String!
convert(coordinate: Double, outputType: Int)

Converts a coordinate to a String representation.

open static Double
convert(coordinate: String!)

Converts a String in one of the formats described by FORMAT_DEGREES, FORMAT_MINUTES, or FORMAT_SECONDS into a double.

open Int

open static Unit
distanceBetween(startLatitude: Double, startLongitude: Double, endLatitude: Double, endLongitude: Double, results: FloatArray!)

Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.

open Float

Returns the approximate distance in meters between this location and the given location.

open Unit
dump(pw: Printer!, prefix: String!)

open Float

Get the estimated horizontal accuracy of this location, radial, in meters.

open Double

Get the altitude if available, in meters above the WGS 84 reference ellipsoid.

open Float

Get the bearing, in degrees.

open Float

Get the estimated bearing accuracy of this location, in degrees.

open Long

Return the time of this fix, in elapsed real-time since system boot.

open Double

Get estimate of the relative precision of the alignment of the ElapsedRealtimeNanos timestamp, with the reported measurements in nanoseconds (68% confidence).

open Bundle!

Returns additional provider-specific information about the location fix as a Bundle.

open Double

Get the latitude, in degrees.

open Double

Get the longitude, in degrees.

open String!

Returns the name of the provider that generated this fix.

open Float

Get the speed if it is available, in meters/second over ground.

open Float

Get the estimated speed accuracy of this location, in meters per second.

open Long

Return the UTC time of this fix, in milliseconds since January 1, 1970.

open Float

Get the estimated vertical accuracy of this location, in meters.

open Boolean

True if this location has a horizontal accuracy.

open Boolean

True if this location has an altitude.

open Boolean

True if this location has a bearing.

open Boolean

True if this location has a bearing accuracy.

open Boolean

True if this location has a elapsed realtime accuracy.

open Boolean

True if this location has a speed.

open Boolean

True if this location has a speed accuracy.

open Boolean

True if this location has a vertical accuracy.

open Boolean

Returns true if the Location came from a mock provider.

open Unit

Remove the horizontal accuracy from this location.

open Unit

Remove the altitude from this location.

open Unit

Remove the bearing from this location.

open Unit

Remove the speed from this location.

open Unit

Clears the contents of the location.

open Unit

Sets the contents of the location to the values from the given location.

open Unit
setAccuracy(horizontalAccuracy: Float)

Set the estimated horizontal accuracy of this location, meters.

open Unit
setAltitude(altitude: Double)

Set the altitude, in meters above the WGS 84 reference ellipsoid.

open Unit
setBearing(bearing: Float)

Set the bearing, in degrees.

open Unit
setBearingAccuracyDegrees(bearingAccuracyDegrees: Float)

Set the estimated bearing accuracy of this location, degrees.

open Unit

Set the time of this fix, in elapsed real-time since system boot.

open Unit

Set estimate of the relative precision of the alignment of the ElapsedRealtimeNanos timestamp, with the reported measurements in nanoseconds (68% confidence).

open Unit
setExtras(extras: Bundle!)

Sets the extra information associated with this fix to the given Bundle.

open Unit
setLatitude(latitude: Double)

Set the latitude, in degrees.

open Unit
setLongitude(longitude: Double)

Set the longitude, in degrees.

open Unit
setProvider(provider: String!)

Sets the name of the provider that generated this fix.

open Unit
setSpeed(speed: Float)

Set the speed, in meters/second over ground.

open Unit
setSpeedAccuracyMetersPerSecond(speedAccuracyMeterPerSecond: Float)

Set the estimated speed accuracy of this location, meters per second.

open Unit
setTime(time: Long)

Set the UTC time of this fix, in milliseconds since January 1, 1970.

open Unit
setVerticalAccuracyMeters(verticalAccuracyMeters: Float)

Set the estimated vertical accuracy of this location, meters.

open String

open Unit
writeToParcel(parcel: Parcel!, flags: Int)

Properties

static Parcelable.Creator<Location!>

Constants

FORMAT_DEGREES

Added in API level 1
static val FORMAT_DEGREES: Int

Constant used to specify formatting of a latitude or longitude in the form "[+-]DDD.DDDDD where D indicates degrees.

Value: 0

FORMAT_MINUTES

Added in API level 1
static val FORMAT_MINUTES: Int

Constant used to specify formatting of a latitude or longitude in the form "[+-]DDD:MM.MMMMM" where D indicates degrees and M indicates minutes of arc (1 minute = 1/60th of a degree).

Value: 1

FORMAT_SECONDS

Added in API level 1
static val FORMAT_SECONDS: Int

Constant used to specify formatting of a latitude or longitude in the form "DDD:MM:SS.SSSSS" where D indicates degrees, M indicates minutes of arc, and S indicates seconds of arc (1 minute = 1/60th of a degree, 1 second = 1/3600th of a degree).

Value: 2

Public constructors

<init>

Added in API level 1
Location(provider: String!)

Construct a new Location with a named provider.

By default time, latitude and longitude are 0, and the location has no bearing, altitude, speed, accuracy or extras.

Parameters
provider String!: the source that provides the location. It can be of type LocationManager#GPS_PROVIDER, LocationManager#NETWORK_PROVIDER, or LocationManager#PASSIVE_PROVIDER. You can also define your own provider string, in which case an empty string is a valid provider.

<init>

Added in API level 1
Location(l: Location!)

Construct a new Location object that is copied from an existing one.

Public methods

bearingTo

Added in API level 1
open fun bearingTo(dest: Location!): Float

Returns the approximate initial bearing in degrees East of true North when traveling along the shortest path between this location and the given location. The shortest path is defined using the WGS84 ellipsoid. Locations that are (nearly) antipodal may produce meaningless results.

Parameters
dest Location!: the destination location
Return
Float the initial bearing in degrees

convert

Added in API level 1
open static fun convert(
    coordinate: Double,
    outputType: Int
): String!

Converts a coordinate to a String representation. The outputType may be one of FORMAT_DEGREES, FORMAT_MINUTES, or FORMAT_SECONDS. The coordinate must be a valid double between -180.0 and 180.0. This conversion is performed in a method that is dependent on the default locale, and so is not guaranteed to round-trip with convert(java.lang.String).

Exceptions
java.lang.IllegalArgumentException if outputType is not one of FORMAT_DEGREES, FORMAT_MINUTES, or FORMAT_SECONDS.

convert

Added in API level 1
open static fun convert(coordinate: String!): Double

Converts a String in one of the formats described by FORMAT_DEGREES, FORMAT_MINUTES, or FORMAT_SECONDS into a double. This conversion is performed in a locale agnostic method, and so is not guaranteed to round-trip with convert(double,int).

Exceptions
java.lang.NullPointerException if coordinate is null
java.lang.IllegalArgumentException if the coordinate is not in one of the valid formats.

describeContents

Added in API level 1
open fun describeContents(): Int
Return
Int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR

distanceBetween

Added in API level 1
open static fun distanceBetween(
    startLatitude: Double,
    startLongitude: Double,
    endLatitude: Double,
    endLongitude: Double,
    results: FloatArray!
): Unit

Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.

The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2].

Parameters
startLatitude Double: the starting latitude
startLongitude Double: the starting longitude
endLatitude Double: the ending latitude
endLongitude Double: the ending longitude
results FloatArray!: an array of floats to hold the results
Exceptions
java.lang.IllegalArgumentException if results is null or has length < 1

distanceTo

Added in API level 1
open fun distanceTo(dest: Location!): Float

Returns the approximate distance in meters between this location and the given location. Distance is defined using the WGS84 ellipsoid.

Parameters
dest Location!: the destination location
Return
Float the approximate distance in meters

dump

Added in API level 3
open fun dump(
    pw: Printer!,
    prefix: String!
): Unit

getAccuracy

Added in API level 1
open fun getAccuracy(): Float

Get the estimated horizontal accuracy of this location, radial, in meters.

We define horizontal accuracy as the radius of 68% confidence. In other words, if you draw a circle centered at this location's latitude and longitude, and with a radius equal to the accuracy, then there is a 68% probability that the true location is inside the circle.

This accuracy estimation is only concerned with horizontal accuracy, and does not indicate the accuracy of bearing, velocity or altitude if those are included in this Location.

If this location does not have a horizontal accuracy, then 0.0 is returned. All locations generated by the LocationManager include horizontal accuracy.

getAltitude

Added in API level 1
open fun getAltitude(): Double

Get the altitude if available, in meters above the WGS 84 reference ellipsoid.

If this location does not have an altitude then 0.0 is returned.

getBearing

Added in API level 1
open fun getBearing(): Float

Get the bearing, in degrees.

Bearing is the horizontal direction of travel of this device, and is not related to the device orientation. It is guaranteed to be in the range (0.0, 360.0] if the device has a bearing.

If this location does not have a bearing then 0.0 is returned.

getBearingAccuracyDegrees

Added in API level 26
open fun getBearingAccuracyDegrees(): Float

Get the estimated bearing accuracy of this location, in degrees.

We define bearing accuracy at 68% confidence. Specifically, as 1-side of the 2-sided range on each side of the estimated bearing reported by getBearing(), within which there is a 68% probability of finding the true bearing.

In the case where the underlying distribution is assumed Gaussian normal, this would be considered 1 standard deviation.

For example, if getBearing() returns 60, and getBearingAccuracyDegrees() returns 10, then there is a 68% probability of the true bearing being between 50 and 70 degrees.

If this location does not have a bearing accuracy, then 0.0 is returned.

getElapsedRealtimeNanos

Added in API level 17
open fun getElapsedRealtimeNanos(): Long

Return the time of this fix, in elapsed real-time since system boot.

This value can be reliably compared to android.os.SystemClock#elapsedRealtimeNanos, to calculate the age of a fix and to compare Location fixes. This is reliable because elapsed real-time is guaranteed monotonic for each system boot and continues to increment even when the system is in deep sleep (unlike getTime.

All locations generated by the LocationManager are guaranteed to have a valid elapsed real-time.

Return
Long elapsed real-time of fix, in nanoseconds since system boot.

getElapsedRealtimeUncertaintyNanos

Added in API level 29
open fun getElapsedRealtimeUncertaintyNanos(): Double

Get estimate of the relative precision of the alignment of the ElapsedRealtimeNanos timestamp, with the reported measurements in nanoseconds (68% confidence). This means that we have 68% confidence that the true timestamp of the event is within ElapsedReatimeNanos +/- uncertainty. Example : - getElapsedRealtimeNanos() returns 10000000 - getElapsedRealtimeUncertaintyNanos() returns 1000000 (equivalent to 1millisecond) This means that the event most likely happened between 9000000 and 11000000.

Return
Double uncertainty of elapsed real-time of fix, in nanoseconds.

getExtras

Added in API level 1
open fun getExtras(): Bundle!

Returns additional provider-specific information about the location fix as a Bundle. The keys and values are determined by the provider. If no additional information is available, null is returned.

A number of common key/value pairs are listed below. Providers that use any of the keys on this list must provide the corresponding value as described below.

  • satellites - the number of satellites used to derive the fix

getLatitude

Added in API level 1
open fun getLatitude(): Double

Get the latitude, in degrees.

All locations generated by the LocationManager will have a valid latitude.

getLongitude

Added in API level 1
open fun getLongitude(): Double

Get the longitude, in degrees.

All locations generated by the LocationManager will have a valid longitude.

getProvider

Added in API level 1
open fun getProvider(): String!

Returns the name of the provider that generated this fix.

Return
String! the provider, or null if it has not been set

getSpeed

Added in API level 1
open fun getSpeed(): Float

Get the speed if it is available, in meters/second over ground.

If this location does not have a speed then 0.0 is returned.

getSpeedAccuracyMetersPerSecond

Added in API level 26
open fun getSpeedAccuracyMetersPerSecond(): Float

Get the estimated speed accuracy of this location, in meters per second.

We define speed accuracy at 68% confidence. Specifically, as 1-side of the 2-sided range above and below the estimated speed reported by getSpeed(), within which there is a 68% probability of finding the true speed.

In the case where the underlying distribution is assumed Gaussian normal, this would be considered 1 standard deviation.

For example, if getSpeed() returns 5, and getSpeedAccuracyMetersPerSecond() returns 1, then there is a 68% probability of the true speed being between 4 and 6 meters per second.

Note that the speed and speed accuracy is often better than would be obtained simply from differencing sequential positions, such as when the Doppler measurements from GNSS satellites are used.

If this location does not have a speed accuracy, then 0.0 is returned.

getTime

Added in API level 1
open fun getTime(): Long

Return the UTC time of this fix, in milliseconds since January 1, 1970.

Note that the UTC time on a device is not monotonic: it can jump forwards or backwards unpredictably. So always use getElapsedRealtimeNanos when calculating time deltas.

On the other hand, getTime is useful for presenting a human readable time to the user, or for carefully comparing location fixes across reboot or across devices.

All locations generated by the LocationManager are guaranteed to have a valid UTC time, however remember that the system time may have changed since the location was generated.

Return
Long time of fix, in milliseconds since January 1, 1970.

getVerticalAccuracyMeters

Added in API level 26
open fun getVerticalAccuracyMeters(): Float

Get the estimated vertical accuracy of this location, in meters.

We define vertical accuracy at 68% confidence. Specifically, as 1-side of the 2-sided range above and below the estimated altitude reported by getAltitude(), within which there is a 68% probability of finding the true altitude.

In the case where the underlying distribution is assumed Gaussian normal, this would be considered 1 standard deviation.

For example, if getAltitude() returns 150, and getVerticalAccuracyMeters() returns 20 then there is a 68% probability of the true altitude being between 130 and 170 meters.

If this location does not have a vertical accuracy, then 0.0 is returned.

hasAccuracy

Added in API level 1
open fun hasAccuracy(): Boolean

True if this location has a horizontal accuracy.

All locations generated by the LocationManager have an horizontal accuracy.

hasAltitude

Added in API level 1
open fun hasAltitude(): Boolean

True if this location has an altitude.

hasBearing

Added in API level 1
open fun hasBearing(): Boolean

True if this location has a bearing.

hasBearingAccuracy

Added in API level 26
open fun hasBearingAccuracy(): Boolean

True if this location has a bearing accuracy.

hasElapsedRealtimeUncertaintyNanos

Added in API level 29
open fun hasElapsedRealtimeUncertaintyNanos(): Boolean

True if this location has a elapsed realtime accuracy.

hasSpeed

Added in API level 1
open fun hasSpeed(): Boolean

True if this location has a speed.

hasSpeedAccuracy

Added in API level 26
open fun hasSpeedAccuracy(): Boolean

True if this location has a speed accuracy.

hasVerticalAccuracy

Added in API level 26
open fun hasVerticalAccuracy(): Boolean

True if this location has a vertical accuracy.

isFromMockProvider

Added in API level 18
open fun isFromMockProvider(): Boolean

Returns true if the Location came from a mock provider.

Return
Boolean true if this Location came from a mock provider, false otherwise

removeAccuracy

Added in API level 1
Deprecated in API level 26
open fun removeAccuracy(): Unit

Deprecated: use a new Location object for location updates.

Remove the horizontal accuracy from this location.

Following this call hasAccuracy will return false, and getAccuracy will return 0.0.

removeAltitude

Added in API level 1
Deprecated in API level 26
open fun removeAltitude(): Unit

Deprecated: use a new Location object for location updates.

Remove the altitude from this location.

Following this call hasAltitude will return false, and getAltitude will return 0.0.

removeBearing

Added in API level 1
Deprecated in API level 26
open fun removeBearing(): Unit

Deprecated: use a new Location object for location updates.

Remove the bearing from this location.

Following this call hasBearing will return false, and getBearing will return 0.0.

removeSpeed

Added in API level 1
Deprecated in API level 26
open fun removeSpeed(): Unit

Deprecated: use a new Location object for location updates.

Remove the speed from this location.

Following this call hasSpeed will return false, and getSpeed will return 0.0.

reset

Added in API level 1
open fun reset(): Unit

Clears the contents of the location.

set

Added in API level 1
open fun set(l: Location!): Unit

Sets the contents of the location to the values from the given location.

setAccuracy

Added in API level 1
open fun setAccuracy(horizontalAccuracy: Float): Unit

Set the estimated horizontal accuracy of this location, meters.

See getAccuracy for the definition of horizontal accuracy.

Following this call hasAccuracy will return true.

setAltitude

Added in API level 1
open fun setAltitude(altitude: Double): Unit

Set the altitude, in meters above the WGS 84 reference ellipsoid.

Following this call hasAltitude will return true.

setBearing

Added in API level 1
open fun setBearing(bearing: Float): Unit

Set the bearing, in degrees.

Bearing is the horizontal direction of travel of this device, and is not related to the device orientation.

The input will be wrapped into the range (0.0, 360.0].

setBearingAccuracyDegrees

Added in API level 26
open fun setBearingAccuracyDegrees(bearingAccuracyDegrees: Float): Unit

Set the estimated bearing accuracy of this location, degrees.

See getBearingAccuracyDegrees for the definition of bearing accuracy.

Following this call hasBearingAccuracy will return true.

setElapsedRealtimeNanos

Added in API level 17
open fun setElapsedRealtimeNanos(time: Long): Unit

Set the time of this fix, in elapsed real-time since system boot.

Parameters
time Long: elapsed real-time of fix, in nanoseconds since system boot.

setElapsedRealtimeUncertaintyNanos

Added in API level 29
open fun setElapsedRealtimeUncertaintyNanos(time: Double): Unit

Set estimate of the relative precision of the alignment of the ElapsedRealtimeNanos timestamp, with the reported measurements in nanoseconds (68% confidence).

Parameters
time Double: uncertainty of the elapsed real-time of fix, in nanoseconds.

setExtras

Added in API level 1
open fun setExtras(extras: Bundle!): Unit

Sets the extra information associated with this fix to the given Bundle.

Note this stores a copy of the given extras, so any changes to extras after calling this method won't be reflected in the location bundle.

setLatitude

Added in API level 1
open fun setLatitude(latitude: Double): Unit

Set the latitude, in degrees.

setLongitude

Added in API level 1
open fun setLongitude(longitude: Double): Unit

Set the longitude, in degrees.

setProvider

Added in API level 1
open fun setProvider(provider: String!): Unit

Sets the name of the provider that generated this fix.

setSpeed

Added in API level 1
open fun setSpeed(speed: Float): Unit

Set the speed, in meters/second over ground.

Following this call hasSpeed will return true.

setSpeedAccuracyMetersPerSecond

Added in API level 26
open fun setSpeedAccuracyMetersPerSecond(speedAccuracyMeterPerSecond: Float): Unit

Set the estimated speed accuracy of this location, meters per second.

See getSpeedAccuracyMetersPerSecond for the definition of speed accuracy.

Following this call hasSpeedAccuracy will return true.

setTime

Added in API level 1
open fun setTime(time: Long): Unit

Set the UTC time of this fix, in milliseconds since January 1, 1970.

Parameters
time Long: UTC time of this fix, in milliseconds since January 1, 1970

setVerticalAccuracyMeters

Added in API level 26
open fun setVerticalAccuracyMeters(verticalAccuracyMeters: Float): Unit

Set the estimated vertical accuracy of this location, meters.

See getVerticalAccuracyMeters for the definition of vertical accuracy.

Following this call hasVerticalAccuracy will return true.

toString

Added in API level 1
open fun toString(): String
Return
String a string representation of the object.

writeToParcel

Added in API level 1
open fun writeToParcel(
    parcel: Parcel!,
    flags: Int
): Unit
Parameters
dest The Parcel in which the object should be written.
flags Int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of android.os.Parcelable#PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES

Properties

CREATOR

Added in API level 1
static val CREATOR: Parcelable.Creator<Location!>