I wrote this code, which should take the user to all waypoints then end the route, but instead it ends when arriving at first point.
Video of issue: https://youtube.com/shorts/MiCmPkNRTkI
As you can see in the video the route ends right when arriving to first waypoint.
Reference: https://developers.google.com/maps/documentation/navigation/android-sdk?hl=pt-br
My Code:
private fun navigateWithWaypoints(waypoints: List<Local>) {
if (waypoints.isEmpty()) {
showToast("Nenhum waypoint disponível para navegação")
return
}
val waypointObjects = waypoints.mapNotNull { local ->
try {
// Criar um Waypoint usando as coordenadas do Local
val (latitude, longitude) = local.coordinates.split(",").map { it.trim().toDouble() }
Waypoint.builder().setLatLng(latitude, longitude).build()
} catch (e: Exception) {
Log.e("NavViewActivity", "Erro ao criar waypoint para ${local.name}: ${e.message}")
null // Ignorar waypoints inválidos
}
}
if (waypointObjects.isEmpty()) {
showToast("Nenhum waypoint válido encontrado")
return
}
// Usar o Navigator para configurar os waypoints e iniciar a navegação
withNavigatorAsync {
val pendingRoute: ListenableResultFuture<RouteStatus> = navigator.setDestinations(waypointObjects);
pendingRoute.setOnResultListener { code ->
when (code) {
RouteStatus.OK -> {
actionBar?.hide() // Ocultar barra de ações
navigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
// Simular navegação (para testes/debug)
if (BuildConfig.DEBUG) {
navigator.simulator.simulateLocationsAlongExistingRoute(
SimulationOptions().speedMultiplier(5f)
)
}
// Iniciar a navegação
navigator.startGuidance()
}
RouteStatus.ROUTE_CANCELED -> showToast("Navegação cancelada")
RouteStatus.NO_ROUTE_FOUND,
RouteStatus.NETWORK_ERROR -> showToast("Erro ao determinar rota: $code")
else -> showToast("Erro iniciando navegação: $code")
}
}
}
}
I tried GPT, searching online and the documentation https://developers.google.com/maps/documentation/navigation/android-sdk/multi-destination?hl=pt-br but didn't get the answer.
Thanks in advance.
Nobody answered to the question so I kept on it, and later on discovered a mistake I had made when coding the registerNavigationListeners function.
private fun registerNavigationListeners() {
withNavigatorAsync {
arrivalListener = Navigator.ArrivalListener {
// Check if there are remaining waypoints
val remainingWaypoints = waypoints.count()
var indexCurrent = 0
if (remainingWaypoints == 0) {
showToast("ROTA FINALIZADA")
// ==>>> HERE WAS THE ERROR, THIS WAS NOT COVERED BY
// ==>>> AN IF STATEMENT WHICH MADE NAVIGATION STOP WITHOUT
// ==>>> ACTUALLY FINISHING THE ROUTE.
navigator.clearDestinations()
// Stop simulating vehicle movement
if (BuildConfig.DEBUG) {
navigator.simulator?.unsetUserLocation()
}
} else {
// Continue to the next waypoint
showToast("PEGAR: ${waypoints[indexCurrent].name.uppercase()}")
indexCurrent += 1
navigator.continueToNextDestination()
waypoints.removeAt(0)
}
}
navigator.addArrivalListener(arrivalListener)
routeChangedListener = Navigator.RouteChangedListener {
showToast("SAIU DO TRAJETO")
}
navigator.addRouteChangedListener(routeChangedListener)
}
}
Thats it, just a little mistake I had made in another piece of the code.