public final class LocationComponent
extends java.lang.Object
RenderMode.NORMAL
simply shows the users location on the map
represented as a dot. RenderMode.COMPASS
mode allows you to display an arrow icon
(by default) that points in the direction the device is pointing in.
RenderMode.GPS
can be used in conjunction with our Navigation SDK to
display a larger icon (customized with LocationComponentOptions.gpsDrawable()
) we call the user puck.
This component also offers the ability to set a map camera behavior for tracking the user
location. These different CameraMode
s will track, stop tracking the location based on the
mode set with setCameraMode(int)
.
To get the component object use DingiMap.getLocationComponent()
and activate it with
activateLocationComponent(Context)
or one of the overloads.
Then, manage its visibility with setLocationComponentEnabled(boolean)
.
Using this component requires you to request permission beforehand manually
This component offers a default, built-in LocationEngine
with some of the activation methods.
for more precise location updates, simply add the Google Play Location Services dependency in your build script.
When activating the component for the first time, the map's max/min zoom levels will be set to
LocationComponentOptions.MAX_ZOOM_DEFAULT
and LocationComponentOptions.MIN_ZOOM_DEFAULT
respectively.
You can adjust the zoom range with LocationComponentOptions.maxZoom()
and
LocationComponentOptions.minZoom()
.
Location Component doesn't support state saving out-of-the-box.
Constructor and Description |
---|
LocationComponent(DingiMap dingiMap)
Internal use.
|
Modifier and Type | Method and Description |
---|---|
void |
activateLocationComponent(android.content.Context context)
This method initializes the component and needs to be called before any other operations are performed.
|
void |
activateLocationComponent(android.content.Context context,
boolean useDefaultLocationEngine)
This method initializes the component and needs to be called before any other operations are performed.
|
void |
activateLocationComponent(android.content.Context context,
int styleRes)
This method initializes the component and needs to be called before any other operations are performed.
|
void |
activateLocationComponent(android.content.Context context,
LocationComponentOptions options)
This method initializes the component and needs to be called before any other operations are performed.
|
void |
activateLocationComponent(android.content.Context context,
com.mapbox.android.core.location.LocationEngine locationEngine)
This method will show the location icon and enable the camera tracking the location.
|
void |
activateLocationComponent(android.content.Context context,
com.mapbox.android.core.location.LocationEngine locationEngine,
int styleRes)
This method initializes the component and needs to be called before any other operations are performed.
|
void |
activateLocationComponent(android.content.Context context,
com.mapbox.android.core.location.LocationEngine locationEngine,
LocationComponentOptions options)
This method initializes the component and needs to be called before any other operations are performed.
|
void |
addCompassListener(CompassListener compassListener)
Add a compass listener to get heading updates every second.
|
void |
addOnCameraTrackingChangedListener(OnCameraTrackingChangedListener listener)
Adds a listener that gets invoked when camera tracking state changes.
|
void |
addOnLocationClickListener(OnLocationClickListener listener)
Adds a listener that gets invoked when the user clicks the displayed location.
|
void |
addOnLocationLongClickListener(OnLocationLongClickListener listener)
Adds a listener that gets invoked when the user long clicks the displayed location.
|
void |
addOnLocationStaleListener(OnLocationStaleListener listener)
Adds the passed listener that gets invoked when user updates have stopped long enough for the last update
to be considered stale.
|
void |
applyStyle(android.content.Context context,
int styleRes)
Apply a new component style with a style resource.
|
void |
applyStyle(LocationComponentOptions options)
Apply a new component style with location component options.
|
void |
cancelTiltWhileTrackingAnimation()
Cancels animation started by
tiltWhileTracking(double, long, DingiMap.CancelableCallback) . |
void |
cancelZoomWhileTrackingAnimation()
Cancels animation started by
zoomWhileTracking(double, long, DingiMap.CancelableCallback) . |
void |
forceLocationUpdate(android.location.Location location)
Use to either force a location update or to manually control when the user location gets
updated.
|
int |
getCameraMode()
Provides the current camera mode being used to track the location or compass updates.
|
CompassEngine |
getCompassEngine()
Returns the compass engine used to provide compass heading values.
|
float |
getLastKnownCompassAccuracyStatus()
Return the last known
CompassEngine accuracy status of the location component. |
android.location.Location |
getLastKnownLocation()
Get the last know location of the location component.
|
LocationComponentOptions |
getLocationComponentOptions()
Returns the current location options being used.
|
com.mapbox.android.core.location.LocationEngine |
getLocationEngine()
Returns the current
LocationEngine being used for updating the user location. |
int |
getRenderMode()
Provides the current render mode being used to show
the location and/or compass updates on the map.
|
boolean |
isLocationComponentEnabled()
Returns whether the plugin is enabled, meaning that location can be displayed and camera modes can be used.
|
void |
onDestroy()
Internal use.
|
void |
onFinishLoadingStyle()
Internal use.
|
void |
onStart()
Internal use.
|
void |
onStartLoadingMap()
Internal use.
|
void |
onStop()
Internal use.
|
void |
removeCompassListener(CompassListener compassListener)
Remove a compass listener.
|
void |
removeOnCameraTrackingChangedListener(OnCameraTrackingChangedListener listener)
Removes a listener that gets invoked when camera tracking state changes.
|
void |
removeOnLocationClickListener(OnLocationClickListener listener)
Removes the passed listener from the current list of location click listeners.
|
void |
removeOnLocationLongClickListener(OnLocationLongClickListener listener)
Removes the passed listener from the current list of location long click listeners.
|
void |
removeOnLocationStaleListener(OnLocationStaleListener listener)
Removes the passed listener from the current list of stale listeners.
|
void |
setCameraMode(int cameraMode)
Sets the camera mode, which determines how the map camera will track the rendered location.
|
void |
setCompassEngine(CompassEngine compassEngine)
Sets the compass engine used to provide compass heading values.
|
void |
setLocationComponentEnabled(boolean isEnabled)
Manage component's visibility after activation.
|
void |
setLocationEngine(com.mapbox.android.core.location.LocationEngine locationEngine)
Set the location engine to update the current user location.
|
void |
setRenderMode(int renderMode)
Sets the render mode, which determines how the location updates will be rendered on the map.
|
void |
tiltWhileTracking(double tilt)
Tilts the camera.
|
void |
tiltWhileTracking(double tilt,
long animationDuration)
Tilts the camera.
|
void |
tiltWhileTracking(double tilt,
long animationDuration,
DingiMap.CancelableCallback callback)
Tilts the camera.
|
void |
zoomWhileTracking(double zoomLevel)
Zooms to the desired zoom level.
|
void |
zoomWhileTracking(double zoomLevel,
long animationDuration)
Zooms to the desired zoom level.
|
void |
zoomWhileTracking(double zoomLevel,
long animationDuration,
DingiMap.CancelableCallback callback)
Zooms to the desired zoom level.
|
public LocationComponent(@NonNull DingiMap dingiMap)
To get the component object use DingiMap.getLocationComponent()
.
public void activateLocationComponent(@NonNull android.content.Context context)
setLocationComponentEnabled(boolean)
.
Note: This method will initialize and use an internal LocationEngine
when enabled.
context
- the contextpublic void activateLocationComponent(@NonNull android.content.Context context, boolean useDefaultLocationEngine)
setLocationComponentEnabled(boolean)
.context
- the contextuseDefaultLocationEngine
- true if you want to initialize and use the built-in location engine or false if
there should be no location engine initializedpublic void activateLocationComponent(@NonNull android.content.Context context, @StyleRes int styleRes)
setLocationComponentEnabled(boolean)
.
Note: This method will initialize and use an internal LocationEngine
when enabled.
context
- the contextstyleRes
- the LocationComponent style respublic void activateLocationComponent(@NonNull android.content.Context context, @NonNull LocationComponentOptions options)
setLocationComponentEnabled(boolean)
.
Note: This method will initialize and use an internal LocationEngine
when enabled.
context
- the contextoptions
- the optionspublic void activateLocationComponent(@NonNull android.content.Context context, @Nullable com.mapbox.android.core.location.LocationEngine locationEngine, @StyleRes int styleRes)
setLocationComponentEnabled(boolean)
.context
- the contextlocationEngine
- the engine, or null if you'd like to only force location updatesstyleRes
- the LocationComponent style respublic void activateLocationComponent(@NonNull android.content.Context context, @NonNull com.mapbox.android.core.location.LocationEngine locationEngine)
context
- the contextlocationEngine
- the enginepublic void activateLocationComponent(@NonNull android.content.Context context, @Nullable com.mapbox.android.core.location.LocationEngine locationEngine, @NonNull LocationComponentOptions options)
setLocationComponentEnabled(boolean)
.locationEngine
- the engine, or null if you'd like to only force location updatesoptions
- the optionspublic void setLocationComponentEnabled(boolean isEnabled)
isEnabled
- true if the plugin should be visible and listen for location updates, false otherwise.public boolean isLocationComponentEnabled()
public void setCameraMode(int cameraMode)
CameraMode.NONE
: No camera trackingCameraMode.NONE_COMPASS
: Camera does not track location, but does track compass bearingCameraMode.NONE_GPS
: Camera does not track location, but does track GPS bearingCameraMode.TRACKING
: Camera tracks the user locationCameraMode.TRACKING_COMPASS
: Camera tracks the user location, with bearing provided by a compassCameraMode.TRACKING_GPS
: Camera tracks the user location, with normalized bearingCameraMode.TRACKING_GPS_NORTH
: Camera tracks the user location, with bearing always set to northcameraMode
- one of the modes found in CameraMode
public int getCameraMode()
public void setRenderMode(int renderMode)
RenderMode.NORMAL
: Shows user location, bearing ignoredRenderMode.COMPASS
: Shows user location with bearing considered from compassRenderMode.GPS
: Shows user location with bearing considered from locationrenderMode
- one of the modes found in RenderMode
public int getRenderMode()
public LocationComponentOptions getLocationComponentOptions()
LocationComponentOptions
public void applyStyle(@NonNull android.content.Context context, @StyleRes int styleRes)
styleRes
- a XML style overriding some or all the optionspublic void applyStyle(@NonNull LocationComponentOptions options)
options
- to update the current stylepublic void zoomWhileTracking(double zoomLevel, long animationDuration, @Nullable DingiMap.CancelableCallback callback)
CameraMode.NONE
.
If you are not using any of CameraMode
modes,
use one of DingiMap.moveCamera(CameraUpdate)
,
DingiMap.easeCamera(CameraUpdate)
or DingiMap.animateCamera(CameraUpdate)
instead.zoomLevel
- The desired zoom level.animationDuration
- The zoom animation duration.callback
- The callback with finish/cancel informationpublic void zoomWhileTracking(double zoomLevel, long animationDuration)
CameraMode.NONE
.
If you are not using any of CameraMode
modes,
use one of DingiMap.moveCamera(CameraUpdate)
,
DingiMap.easeCamera(CameraUpdate)
or DingiMap.animateCamera(CameraUpdate)
instead.zoomLevel
- The desired zoom level.animationDuration
- The zoom animation duration.public void zoomWhileTracking(double zoomLevel)
CameraMode.NONE
.
If you are not using any of CameraMode
modes,
use one of DingiMap.moveCamera(CameraUpdate)
,
DingiMap.easeCamera(CameraUpdate)
or DingiMap.animateCamera(CameraUpdate)
instead.zoomLevel
- The desired zoom level.public void cancelZoomWhileTrackingAnimation()
zoomWhileTracking(double, long, DingiMap.CancelableCallback)
.public void tiltWhileTracking(double tilt, long animationDuration, @Nullable DingiMap.CancelableCallback callback)
CameraMode.NONE
.
If you are not using any of CameraMode
modes,
use one of DingiMap.moveCamera(CameraUpdate)
,
DingiMap.easeCamera(CameraUpdate)
or DingiMap.animateCamera(CameraUpdate)
instead.tilt
- The desired camera tilt.animationDuration
- The tilt animation duration.callback
- The callback with finish/cancel informationpublic void tiltWhileTracking(double tilt, long animationDuration)
CameraMode.NONE
.
If you are not using any of CameraMode
modes,
use one of DingiMap.moveCamera(CameraUpdate)
,
DingiMap.easeCamera(CameraUpdate)
or DingiMap.animateCamera(CameraUpdate)
instead.tilt
- The desired camera tilt.animationDuration
- The tilt animation duration.public void tiltWhileTracking(double tilt)
CameraMode.NONE
.
If you are not using any of CameraMode
modes,
use one of DingiMap.moveCamera(CameraUpdate)
,
DingiMap.easeCamera(CameraUpdate)
or DingiMap.animateCamera(CameraUpdate)
instead.tilt
- The desired camera tilt.public void cancelTiltWhileTrackingAnimation()
tiltWhileTracking(double, long, DingiMap.CancelableCallback)
.public void forceLocationUpdate(@Nullable android.location.Location location)
location
- where the location icon is placed on the mappublic void setLocationEngine(@Nullable com.mapbox.android.core.location.LocationEngine locationEngine)
If null
is passed in, all updates will have to occur through the
forceLocationUpdate(Location)
method.
locationEngine
- a LocationEngine
this component should use to handle updates@Nullable public com.mapbox.android.core.location.LocationEngine getLocationEngine()
LocationEngine
being used for updating the user location.LocationEngine
being used to update the user locationpublic void setCompassEngine(@NonNull CompassEngine compassEngine)
compassEngine
- to be used@NonNull public CompassEngine getCompassEngine()
@Nullable public android.location.Location getLastKnownLocation()
public float getLastKnownCompassAccuracyStatus()
CompassEngine
accuracy status of the location component.
The last known accuracy of the compass sensor, one of SensorManager.SENSOR_STATUS_*
public void addCompassListener(@NonNull CompassListener compassListener)
compassListener
- a CompassListener
for listening into compass heading and
accuracy changespublic void removeCompassListener(@NonNull CompassListener compassListener)
compassListener
- the CompassListener
which you'd like to remove from the listener
list.public void addOnLocationClickListener(@NonNull OnLocationClickListener listener)
If there are registered location click listeners and the location is clicked,
only OnLocationClickListener.onLocationComponentClick()
is going to be delivered,
DingiMap.OnMapClickListener#onMapClick(LatLng)
is going to be consumed
and not pushed to the listeners registered after the component's activation.
listener
- The location click listener that is invoked when the
location is clickedpublic void removeOnLocationClickListener(@NonNull OnLocationClickListener listener)
listener
- to be removedpublic void addOnLocationLongClickListener(@NonNull OnLocationLongClickListener listener)
If there are registered location long click listeners and the location is long clicked,
only OnLocationLongClickListener.onLocationComponentLongClick()
is going to be delivered,
DingiMap.OnMapLongClickListener#onMapLongClick(LatLng)
is going to be consumed
and not pushed to the listeners registered after the component's activation.
listener
- The location click listener that is invoked when the
location is clickedpublic void removeOnLocationLongClickListener(@NonNull OnLocationLongClickListener listener)
listener
- to be removedpublic void addOnCameraTrackingChangedListener(@NonNull OnCameraTrackingChangedListener listener)
listener
- Listener that gets invoked when camera tracking state changes.public void removeOnCameraTrackingChangedListener(@NonNull OnCameraTrackingChangedListener listener)
listener
- Listener that gets invoked when camera tracking state changes.public void addOnLocationStaleListener(@NonNull OnLocationStaleListener listener)
This timeout is set by LocationComponentOptions.staleStateTimeout()
.
listener
- invoked when last update is considered stalepublic void removeOnLocationStaleListener(@NonNull OnLocationStaleListener listener)
listener
- to be removed from the listpublic void onStart()
public void onStop()
public void onDestroy()
public void onStartLoadingMap()
public void onFinishLoadingStyle()