Skip to main content

Android Native - 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 CoreConstants.USER_LOCATION_PROJECTION_ON_ITINERARY_ENABLED constant.

  • If this constant 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 constant 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

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.activity.ComponentActivity
import com.getwemap.sdk.core.internal.CoreConstants
import com.getwemap.sdk.core.model.ServiceFactory
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 com.getwemap.sdk.core.model.services.IItineraryService
import com.getwemap.sdk.core.model.services.parameters.ItineraryParameters
import com.getwemap.sdk.core.navigation.NavigationInfoHandler
import io.reactivex.rxjava3.disposables.CompositeDisposable

class MainActivity : ComponentActivity() {

private val disposeBag = CompositeDisposable()
private val service = ServiceFactory.getItineraryService()

private val infoHandler = NavigationInfoHandler()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
computeItineraries()
}

private fun computeItineraries() {
val origin = Coordinate(43.610628, 3.876654)
val destination = Coordinate(43.609011, 3.917091)

val parameters = ItineraryParameters(origin, destination, TravelMode.Bike(TravelMode.Preference.FASTEST))

val disposable = service
.itineraries(parameters)
.subscribe({
println("response - $it")
setupItinerary(it.itineraries.first())
}, {
println("error - $it")
})
disposeBag.add(disposable)
}

private fun setupItinerary(itinerary: Itinerary) {
infoHandler.itinerary = itinerary

// request for an update of the navigation information with a certain time interval
Handler(Looper.getMainLooper()).postDelayed({
getNavigationInfo(Coordinate(43.610107, 3.891805))
}, 5000)
}

private fun getNavigationInfo(userPosition: Coordinate) {
val info = infoHandler.getNavigationInfo(userPosition)
println("navigation info - $info")
}
}

WemapMapSDK

In WemapMapSDK this logic is controlled by IMapNavigationManager 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 constantly calls NavigationManagerListener.onNavigationInfoChanged(info: NavigationInfo) method with the updated NavigationInfo object.

You can set NavigationInfo update time interval using mapView.navigationManager.infoUpdatePeriod = 2.

import android.os.Bundle
import android.view.ViewGroup.LayoutParams
import androidx.appcompat.app.AppCompatActivity
import com.getwemap.sdk.core.model.entities.Coordinate
import com.getwemap.sdk.core.model.entities.MapData
import com.getwemap.sdk.core.navigation.info.NavigationInfo
import com.getwemap.sdk.core.navigation.manager.NavigationManagerListener
import com.getwemap.sdk.map.OnMapViewReadyCallback
import com.getwemap.sdk.map.WemapMapSDK
import com.getwemap.sdk.map.WemapMapView
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.CompositeDisposable
import org.maplibre.android.MapLibre
import org.maplibre.android.location.modes.CameraMode
import org.maplibre.android.location.modes.RenderMode
import org.maplibre.android.maps.MapLibreMap
import org.maplibre.android.maps.Style

class NavigationInfoActivity: AppCompatActivity(), OnMapViewReadyCallback, NavigationManagerListener {

private lateinit var mapView: WemapMapView
private val disposeBag = CompositeDisposable()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

MapLibre.getInstance(applicationContext)

val mapLoad = WemapMapSDK.instance
.mapData(19158, "GUHTU6TYAWWQHUSR5Z5JZNMXX")
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ mapData ->

mapView = WemapMapView(applicationContext, null)
mapView.mapData = mapData
mapView.getMapViewAsync(this)

addContentView(mapView, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
}, {
println("Failed to get map data with error - $it")
})
disposeBag.add(mapLoad)
}

private fun startNavigation() {
val origin = Coordinate(43.609011, 3.917091)
val destination = Coordinate(43.610628, 3.876654)

val navLoad = mapView.navigationManager
.startNavigation(origin, destination)
.subscribe({ _ ->
println("Successfully started navigation from: $origin to: $destination")
}, {
println("Failed to start navigation from $origin to: $destination with error: $it")
})
disposeBag.add(navLoad)
}

override fun onMapViewReady(mapView: WemapMapView, map: MapLibreMap, style: Style, data: MapData) {
mapView.navigationManager.addListener(this)
mapView.navigationManager.infoUpdatePeriod = 2 // default is 1 second
mapView.locationManager.apply {
isEnabled = true
renderMode = RenderMode.COMPASS
cameraMode = CameraMode.TRACKING_COMPASS
}
startNavigation()
}

override fun onNavigationInfoChanged(info: NavigationInfo) {
println("Navigation info updated - $info")
}
}

Examples

You can find additional examples for the WemapSDKs on GitHub. Clone the repository and run the example application following the instructions in the README.