LocationSource
Stay organized with collections
Save and categorize content based on your preferences.
Defines an interface for providing location data, typically to a GoogleMap
object.
A GoogleMap
object
has a built-in location provider for its my-location layer, but it can be replaced
with another one that implements this interface.
A GoogleMap
object
activates its location provider using activate(OnLocationChangedListener)
. While active (between activate(OnLocationChangedListener)
and deactivate()
),
a location provider should push periodic location updates to the listener registered in activate(OnLocationChangedListener)
. It is the provider's responsibility to use
location services wisely according to the map's lifecycle state. For example, it should only
using battery-intensive services (like GPS) occasionally, or only while an activity is in the
foreground.
Nested Class Summary
interface
Handles a location update.
Public Method Summary
abstract void |
|
abstract void |
|
Public Methods
Activates this provider. This provider will notify the supplied listener
periodically, until you call deactivate()
.
Notifications will be broadcast on the main thread.
Parameters
listener
listener that's called when a new location is available
Throws
public abstract void
deactivate
()
Deactivates this provider. The previously-registered callback is not notified of any
further updates.
Throws
if this provider is already inactive
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[[["\u003cp\u003e\u003ccode\u003eLocationSource\u003c/code\u003e provides location data, often to a \u003ccode\u003eGoogleMap\u003c/code\u003e object, offering an alternative to the built-in location provider.\u003c/p\u003e\n"],["\u003cp\u003eThis interface uses \u003ccode\u003eactivate()\u003c/code\u003e and \u003ccode\u003edeactivate()\u003c/code\u003e methods to manage the location provider's lifecycle and push location updates.\u003c/p\u003e\n"],["\u003cp\u003eWhen activated, the provider sends periodic location updates to a registered listener, ensuring efficient use of location services.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers implementing \u003ccode\u003eLocationSource\u003c/code\u003e should consider the map's lifecycle to optimize battery consumption and resource usage.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eOnLocationChangedListener\u003c/code\u003e nested interface handles location updates broadcasted on the main thread.\u003c/p\u003e\n"]]],[],null,["# LocationSource\n\npublic interface **LocationSource** \nDefines an interface for providing location data, typically to a [GoogleMap](/android/reference/com/google/android/gms/maps/GoogleMap)\nobject.\n\nA [GoogleMap](/android/reference/com/google/android/gms/maps/GoogleMap) object\nhas a built-in location provider for its my-location layer, but it can be [replaced](/android/reference/com/google/android/gms/maps/GoogleMap#setLocationSource(com.google.android.gms.maps.LocationSource)) with another one that implements this interface.\n\nA [GoogleMap](/android/reference/com/google/android/gms/maps/GoogleMap) object\nactivates its location provider using [activate(OnLocationChangedListener)](/android/reference/com/google/android/gms/maps/LocationSource#activate(com.google.android.gms.maps.LocationSource.OnLocationChangedListener)). While active (between [activate(OnLocationChangedListener)](/android/reference/com/google/android/gms/maps/LocationSource#activate(com.google.android.gms.maps.LocationSource.OnLocationChangedListener)) and [deactivate()](/android/reference/com/google/android/gms/maps/LocationSource#deactivate())),\na location provider should push periodic location updates to the listener registered in\n[activate(OnLocationChangedListener)](/android/reference/com/google/android/gms/maps/LocationSource#activate(com.google.android.gms.maps.LocationSource.OnLocationChangedListener)). It is the provider's responsibility to use\nlocation services wisely according to the map's lifecycle state. For example, it should only\nusing battery-intensive services (like GPS) occasionally, or only while an activity is in the\nforeground. \n\n### Nested Class Summary\n\n|-----------|---|---|----------------------------|\n| interface | [LocationSource.OnLocationChangedListener](/android/reference/com/google/android/gms/maps/LocationSource.OnLocationChangedListener) || Handles a location update. |\n\n### Public Method Summary\n\n|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [activate](/android/reference/com/google/android/gms/maps/LocationSource#activate(com.google.android.gms.maps.LocationSource.OnLocationChangedListener))([LocationSource.OnLocationChangedListener](/android/reference/com/google/android/gms/maps/LocationSource.OnLocationChangedListener) listener) Activates this provider. |\n| abstract void | [deactivate](/android/reference/com/google/android/gms/maps/LocationSource#deactivate())() Deactivates this provider. |\n\nPublic Methods\n--------------\n\n#### public abstract void **activate** ([LocationSource.OnLocationChangedListener](/android/reference/com/google/android/gms/maps/LocationSource.OnLocationChangedListener) listener)\n\nActivates this provider. This provider will notify the supplied listener\nperiodically, until you call [deactivate()](/android/reference/com/google/android/gms/maps/LocationSource#deactivate()).\nNotifications will be broadcast on the main thread. \n\n##### Parameters\n\n| listener | listener that's called when a new location is available |\n|----------|---------------------------------------------------------|\n\n##### Throws\n\n| [IllegalStateException](//developer.android.com/reference/java/lang/IllegalStateException.html) | if this provider is already active |\n| [IllegalArgumentException](//developer.android.com/reference/java/lang/IllegalArgumentException.html) | if `listener` is `null` |\n|-------------------------------------------------------------------------------------------------------|------------------------------------|\n\n#### public abstract void **deactivate** ()\n\nDeactivates this provider. The previously-registered callback is not notified of any\nfurther updates. \n\n##### Throws\n\n| [IllegalStateException](//developer.android.com/reference/java/lang/IllegalStateException.html) | if this provider is already inactive |\n|-------------------------------------------------------------------------------------------------|--------------------------------------|"]]