Navigation information
The NavigationInfo class contains all information about the user's progress along the itinerary, including the current leg and step information. This object allows you to get distance and time measurements, previous and next steps, and more.
The NavigationInfoHandler helps to get updated information about the user’s progress along the itinerary on request by providing an updated NavigationInfo object.
Navigation information is calculated according to the userLocationProjectionOnItineraryEnabled flag of the SessionConfig passed when creating the session.
-
If this flag is enabled, the origin and user projection distances will not be included in the total navigation distance, since these parts are not visible to the user.
-
If this flag is disabled, the origin and user projection distance will be included in the total navigation distance, since these parts are visible to the user. Thus, in this case, the total itinerary distance is not equal to the distance traveled + distance remaining.
Below are code samples showing how to get NavigationInfo using only WemapCoreSDK or WemapMapSDK.
WemapCoreSDK
With WemapCoreSDK you obtain the NavigationInfoHandler from your session (session.navigationInfoHandler),
assign an itinerary to it, then request NavigationInfo for a given user position.
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.lifecycle.lifecycleScope
import com.getwemap.sdk.core.CoreSession
import com.getwemap.sdk.core.model.entities.Coordinate
import com.getwemap.sdk.core.model.entities.Itinerary
import com.getwemap.sdk.core.model.entities.TravelMode
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.seconds
class MainActivity : ComponentActivity() {
private var session: CoreSession? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch {
runCatching {
CoreSession.create(applicationContext, 19158, "GUHTU6TYAWWQHUSR5Z5JZNMXX")
}.onSuccess { session ->
this@MainActivity.session = session
computeItineraries(session)
}.onFailure {
println("Failed to create session with error - $it")
}
}
}
override fun onDestroy() {
super.onDestroy()
session?.deinit()
session = null
}
private fun computeItineraries(session: CoreSession) {
val origin = Coordinate(43.610628, 3.876654)
val destination = Coordinate(43.609011, 3.917091)
lifecycleScope.launch {
runCatching {
session.itineraryProvider.itineraries(origin, destination, TravelMode.Bike(TravelMode.Preference.FASTEST))
}.onSuccess {
println("itineraries - $it")
setupItinerary(session, it.first())
}
.onFailure {
println("error - $it")
}
}
}
private suspend fun setupItinerary(session: CoreSession, itinerary: Itinerary) {
session.navigationInfoHandler.itinerary = itinerary
// request for an update of the navigation information with a certain time interval
delay(5.seconds)
getNavigationInfo(session, Coordinate(43.610107, 3.891805))
}
private fun getNavigationInfo(session: CoreSession, userPosition: Coordinate) {
val info = session.navigationInfoHandler.getNavigationInfo(userPosition)
println("navigation info - $info")
}
}
WemapMapSDK
In WemapMapSDK this logic is controlled by the MapNavigationManager interface, which you can access directly from the map using mapView.navigationManager as shown below.
NavigationManager observes the user’s progress along the itinerary and emits an updated NavigationInfo object through its navigationInfoUpdates Flow, which you collect.
You can set the NavigationInfo update interval using mapView.navigationManager.navigationInfoUpdatesInterval = 2.seconds.
import android.os.Bundle
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.getwemap.sdk.core.model.entities.Coordinate
import com.getwemap.sdk.map.MapSession
import com.getwemap.sdk.map.WemapMapView
import kotlinx.coroutines.launch
import org.maplibre.android.MapLibre
import org.maplibre.android.location.modes.CameraMode
import org.maplibre.android.location.modes.RenderMode
import kotlin.time.Duration.Companion.seconds
class NavigationInfoActivity: AppCompatActivity() {
private lateinit var mapView: WemapMapView
private val navigationManager get() = mapView.navigationManager
private var session: MapSession? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MapLibre.getInstance(applicationContext)
lifecycleScope.launch {
runCatching {
MapSession.create(applicationContext, 19158, "GUHTU6TYAWWQHUSR5Z5JZNMXX")
}.onSuccess { session ->
this@NavigationInfoActivity.session = session
mapView = WemapMapView(applicationContext, null)
lifecycleScope.launch {
runCatching {
mapView.awaitLoaded()
}.onSuccess { loaded ->
onMapViewLoaded(loaded)
}.onFailure { error ->
println("Failed to load mapView with error - $error")
}
}
mapView.configure(session)
val params = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
addContentView(mapView, params)
}.onFailure {
println("Failed to create session with error - $it")
}
}
}
override fun onDestroy() {
super.onDestroy()
session?.deinit()
session = null
}
private fun startNavigation() {
val origin = Coordinate(43.609011, 3.917091)
val destination = Coordinate(43.610628, 3.876654)
lifecycleScope.launch {
runCatching {
navigationManager.startNavigation(origin, destination)
}.onSuccess {
println("Successfully started navigation from: $origin to: $destination")
}.onFailure {
println("Failed to start navigation from $origin to: $destination with error: $it")
}
}
}
private fun onMapViewLoaded(mapView: WemapMapView) {
navigationManager.navigationInfoUpdatesInterval = 2.seconds // default is 1 second
mapView.locationManager.apply {
isEnabled = true
renderMode = RenderMode.COMPASS
cameraMode = CameraMode.TRACKING_COMPASS
}
lifecycleScope.launch {
navigationManager.navigationInfoUpdates.collect { info ->
println("Navigation info updated - $info")
}
}
startNavigation()
}
}
Examples
For additional examples and sample implementations of WemapSDKs, visit the official GitHub repository.
Clone the repository and follow the README instructions to run the sample application.