GpsManager
This module simplifies the process of requesting GPS location updates in an Android application and is required to use the Navigation module. It allows users to define multiple GPS sources (for example, the built-in GPS is a source, and a GPS simulation file is another source).
Module GPS Manager
Dependencies
This module depends on other BeNomad's modules :
Error Manager
Core
Settings
Other module's dependencies :
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.0"
implementation "androidx.core:core-ktx:1.9.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.8.10"
implementation "androidx.appcompat:appcompat:1.3.0"
implementation "com.google.android.material:material:1.8.0"Before using any example below, you must request access to the required permissions for the source that you will use. You can do this using the Permissions Manager module (sse the documentation of the Permissions Manager module for more details about its implementation) :
import com.benomad.permissionsmanager.*
val source = LocationFromBuiltInGPS(context) //Built-in GPS source of the Android device.
PermissionsManager.requestPermissions(source.getRequiredPermissions())
*Note : Each source has it own implementation of the getRequiredPermissions method.*Getting location updates from the built-in GPS
import com.benomad.msdk.gps.*
val source = LocationFromBuiltInGPS(context)
if(source.isFeatureEnabled()) {
// The phone GPS feature is enabled
if (GPSManager.getInstance().start(source)) {
// Get the current user location
val currentPosition = GPSManager.getInstance().getLastKnownLocation()
} else {
// Cannot get location data from built-in GPS
}
} else {
// The built-in GPS is disabled, we should ask the user to turn it on in an AlertDialog for example.
// Use this to open GPS settings of the user's phone
startActivity(source.getIntentForGPSActivation())
}Getting location updates from a GPS simulation file
import com.benomad.msdk.gps.*
val gpsFile = File (Environment.getExternalStorageDirectory()+"../fakegps.nmea"
//the path to the fake GPS file
val source = LocationFromNMEAFile(gpsFile)
if(source.isFeatureEnabled()) {
if (GPSManager.getInstance().start(source)) {
// Get the current user location
val currentPosition = GPSManager.getInstance().getLastKnownLocation()
} else {
// Cannot start location updates using the provided GPS simulation file
}
} else {
// GPS simulation file couldn't be loaded as a source
}
...Getting location updates from a planned Route (Demo/Simulation mode)
Another way is to use a planned route (computed with any Planner function) and use it to get the GPS data.
This is also called simulation or demo mode, as it will follow exactly the computed route: the simulation will drive at the max speed of the current road. The simulation speed is configurable, by default it's 100 (in percent) and can be changed in this range 1, 400.
Notes:
The simulation will only work when a navigation is running, meaning that a call to
Navigation.startSessionis necessary.When the
Routeis finished, it'll loop from the start until we callNavigation.stopSession.Navigation.startSessioncan be called with a Route (navigation mode) or without it (tracking mode).A new
LocationFromRouteshould be used for each new session to always start from the beginning of theRoute.
import com.benomad.msdk.gps.*
fun initSimulationMode(route: Route) {
val source = LocationFromRoute(route, 100)
if(source.isFeatureEnabled()) {
GPSManager.getInstance().start(source) // The source is now ready to be used by the Navigation
}
}