LocationSource

  • LocationSource is an interface used to provide location data to a GoogleMap object, offering an alternative to the built-in location provider.

  • Developers can implement this interface to create custom location providers and manage how location updates are delivered to the map.

  • The activate() method initiates the location provider, requiring an OnLocationChangedListener to receive location updates.

  • The deactivate() method stops the location provider, ceasing further location updates to the registered listener.

  • Implementing LocationSource allows control over location data frequency and resource usage, such as battery consumption by GPS.

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