I'm new to developing Android apps using Kotlin and XML and with the use of Google Maps Platform Navigation SDK for Android. I have a problem that when the distance from the origin and the destination waypoint is just say about less than or equal to 150 meters, the navigation SDK has already flagged it as arrived, even though the user has not even moved or arrived at said destination. The problem does not fire up when the distance is more than 150 meters. I don't know if it's a bug or what. For reference, here is the code below:
private fun initializeNavigationSdk(routeToken: String, placeIds: ArrayList<String>, travelMode: String) {
// Request location permission.
if (ContextCompat.checkSelfPermission(
applicationContext, Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED) {
locationPermissionGranted = true
} else {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
)
}
if (!locationPermissionGranted) {
displayMessage("Error loading Navigation SDK: The user has not granted location permission.")
return
}
// Get a navigator.
NavigationApi.getNavigator(
this,
object : NavigationApi.NavigatorListener {
override fun onNavigatorReady(navigator: Navigator) {
displayMessage("Navigator ready.")
[email protected] = navigator
navFragment = supportFragmentManager.findFragmentById(R.id.navigation_fragment) as SupportNavigationFragment
navFragment.setForceNightMode(FORCE_NIGHT)
navFragment.setTripProgressBarEnabled(true)
navFragment.setSpeedometerEnabled(true)
navFragment.setSpeedLimitIconEnabled(true)
navFragment.setStylingOptions(StylingOptions()
.headerGuidanceRecommendedLaneColor(resources.getColor(R.color.brand_color))
.primaryDayModeThemeColor(resources.getColor(R.color.secondary_color)))
// Set the camera to follow the device location with 'TILTED' driving view.
navFragment.getMapAsync { googleMap ->
try {
if (ContextCompat.checkSelfPermission(
applicationContext,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
googleMap.followMyLocation(CameraPerspective.TILTED)
} else {
displayMessage("Location permission is not granted. Unable to follow location.")
}
} catch (e: SecurityException) {
displayMessage("Error accessing location: ${e.message}")
}
}
navigateWithMultipleStops(routeToken, placeIds, travelMode)
}
override fun onError(@NavigationApi.ErrorCode errorCode: Int) {
when (errorCode) {
NavigationApi.ErrorCode.NOT_AUTHORIZED -> displayMessage("Your API key is invalid or not authorized to use the Navigation SDK.")
NavigationApi.ErrorCode.TERMS_NOT_ACCEPTED -> displayMessage("Please accept the terms and conditions to continue.")
NavigationApi.ErrorCode.NETWORK_ERROR -> displayMessage("Error loading Navigation SDK: Network error.")
NavigationApi.ErrorCode.LOCATION_PERMISSION_MISSING -> displayMessage("Error loading Navigation SDK: Location permission is missing.")
else -> displayMessage("Error loading Navigation SDK: $errorCode")
}
finish()
}
}
)
}
private fun navigateWithMultipleStops(routeToken: String, placesIds: List<String>, travelMode: String) {
// Create a list of waypoints based on the provided place IDs
val waypoints = mutableListOf<Waypoint>()
try {
for (placeId in placesIds) {
// Build each waypoint using the placeId and add to the list
val waypoint = Waypoint.builder()
.setPlaceIdString(placeId)
.setVehicleStopover(true)
.build()
waypoints.add(waypoint)
}
} catch (e: Waypoint.UnsupportedPlaceIdException) {
displayMessage("Error starting navigation: One or more Place IDs are not supported.")
return
}
// Set display options for stop signs and traffic lights
val displayOptions = DisplayOptions().apply {
showStopSigns(true)
showTrafficLights(true)
}
val pendingRoute = if (travelMode == "WALK" || travelMode == "TRANSIT") {
val routingOptions = RoutingOptions().apply {
travelMode(if (travelMode == "WALK") TravelMode.WALKING else TravelMode.DRIVING)
avoidFerries(true)
avoidTolls(true)
alternateRoutesStrategy(AlternateRoutesStrategy.SHOW_ALL)
}
Log.e("StartNavigationsActivity", "ROUTE TOKEN: $routeToken")
navigator.setDestinations(waypoints, routingOptions, displayOptions)
} else {
val travelModeTraffic = if (travelMode == "DRIVE") CustomRoutesOptions.TravelMode.DRIVING else CustomRoutesOptions.TravelMode.TWO_WHEELER
val customRoutesOptions = CustomRoutesOptions.builder()
.setRouteToken(routeToken)
.setTravelMode(travelModeTraffic)
.build()
Log.e("StartNavigationsActivity", "ROUTE TOKEN AT ELSE: $routeToken")
navigator.setDestinations(waypoints, customRoutesOptions, displayOptions)
}
handleRouteResult(pendingRoute)
}
private fun handleRouteResult(pendingRoute: ListenableResultFuture<Navigator.RouteStatus>) {
pendingRoute.setOnResultListener { code ->
when (code) {
Navigator.RouteStatus.OK -> {
displayMessage("Route successfully calculated with multiple stops!")
navigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
registerNavigationListeners()
if (isSimulated) {
navigator.simulator.simulateLocationsAlongExistingRoute(SimulationOptions().speedMultiplier(5F))
}
navigator.startGuidance()
startTrip()
}
Navigator.RouteStatus.NO_ROUTE_FOUND -> displayMessage("Error starting navigation: No route found.")
Navigator.RouteStatus.NETWORK_ERROR -> displayMessage("Error starting navigation: Network error.")
Navigator.RouteStatus.ROUTE_CANCELED -> displayMessage("Error starting navigation: Route canceled.")
else -> displayMessage("Error starting navigation: $code")
}
}
}
private fun registerNavigationListeners() {
mArrivalListener = ArrivalListener { arrivalEvent ->
if (arrivalEvent.isFinalDestination) {
displayMessage("onArrival: You've arrived at the final destination.")
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE))
showTripSummaryDialog(computeTotalDistance(), computeTotalTime(), computeArrivalTime(), computeAverageSpeed(),getTrafficConditions())
navigator.simulator.unsetUserLocation()
} else {
// Continue to the next waypoint if not at the final destination
navigator.continueToNextDestination()
navigator.startGuidance()
}
}
// Listens for arrival at a waypoint.
navigator.addArrivalListener(mArrivalListener)
}
I tried determining the remaining meters using navigator.currentTimeAndDistance.meters
but it still does not override the set threshold of Google Maps.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4