LocationSource

  • LocationSource provides location data, often to a GoogleMap object, offering an alternative to the built-in location provider.

  • This interface allows you to activate and deactivate the location provider, controlling when location updates are sent to a registered listener.

  • When activated, the location provider sends periodic updates to the listener, ensuring the efficient use of location services based on the map's state.

  • You can customize how location data is obtained and provided to the map by implementing the LocationSource interface.

  • The OnLocationChangedListener nested class handles location updates, which are broadcast on the main thread.

public interface LocationSource

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 LocationSource.OnLocationChangedListener Handles a location update. 

Public Method Summary

abstract void
activate(LocationSource.OnLocationChangedListener listener)
Activates this provider.
abstract void
deactivate()
Deactivates this provider.

Public Methods

public abstract void activate (LocationSource.OnLocationChangedListener listener)

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
IllegalStateException if this provider is already active
IllegalArgumentException if listener is null

public abstract void deactivate ()

Deactivates this provider. The previously-registered callback is not notified of any further updates.

Throws
IllegalStateException if this provider is already inactive