You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Navigation SDK(Android)使用自定义路线令牌后仍导航至最快路线的问题咨询

Google Navigation SDK(Android)使用自定义路线令牌后仍导航至最快路线的问题咨询

我目前在Android上结合Google Navigation SDK和Routes API的directions/v2:computeRoutes接口进行开发。通过computeRoutes我获取到了两条路线:默认最快路线和一条备选路线(距离更短但耗时更长:57公里/1小时 vs 79公里/52分钟)。我希望使用这条备选路线进行导航,于是提取了它的routeToken并传递给Navigation SDK的导航器,但即使成功兑换令牌并启动导航,SDK仍然会导航到那条最快的高速路线,而非我选择的备选路线。

复现代码

@SuppressLint("MissingPermission")
private fun startNavigationWithRouteToken(originLat: Double, originLng: Double) {
    val token = routeTokenArg ?: return toast("Missing route token")
    if (!::navigator.isInitialized) return toast("Navigator not ready yet")
    if (!mLocationPermissionGranted) return toast("Location permission required")

    val destLat = destLatArg ?: return toast("Missing destination lat")
    val destLng = destLngArg ?: return toast("Missing destination lng")

    val currentLatLng = getCurrentDeviceLatLng()
    val isNearOrigin = currentLatLng != null && metersBetween(
        currentLatLng.latitude, currentLatLng.longitude,
        originLat, originLng
    ) < 100 // Example threshold: 100 meters

    if (isNearOrigin) {
        // User is close to the route's original start. Redeem the token.
        Log.d(TAG, "User is near origin. Attempting to redeem route token.")
        val originWp = Waypoint.Builder().setLatLng(originLat, originLng).build()
        val destWp = Waypoint.Builder().setLatLng(destLat, destLng).build()

        val custom = CustomRoutesOptions.builder()
            .setRouteToken(token)
            .setTravelMode(CustomRoutesOptions.TravelMode.DRIVING)
            .build()

        navigator.setDestinations(listOf(originWp, destWp), custom)
            .setOnResultListener { code ->
                if (code == Navigator.RouteStatus.OK) {
                    Log.d(TAG, "Token redemption successful. Starting guidance.")
                    navigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
                    navigator.startGuidance()
                } else {
                    Log.e(TAG, "Token redemption failed ($code). Falling back to live route.")
                    toast("Failed to use preferred route. Starting live navigation.")
                }
            }
    } else {
        // User is NOT near the origin. Don't use the token;
        Log.d(TAG, "User is not near origin. Starting live navigation without token.")
        toast("Starting live navigation from current location.")
    }
}

预期与实际结果

预期结果

导航器会严格遵循我选择的备选路线对应的routeToken所描述的路径进行导航。

实际结果

导航成功启动,但SDK似乎自动选择了最快的那条路线,而非我指定的备选路线。日志显示令牌兑换流程无异常:

User is near origin. Attempting to redeem route token.
Token redemption successful. Starting guidance.

已尝试的解决方案

  • 直接使用备选路线的routeToken,通过setDestinations(..., CustomRoutesOptions)的方式完成令牌兑换
  • 将备选路线的完整折线转换为大量中途点(stopover waypoints),再调用setDestinations(waypoints, RoutingOptions):这种方式能大致强制走指定路径,但存在诸多问题——容易触发中途点数量限制、实现逻辑脆弱,且完全违背了使用路线令牌的设计初衷

疑问列表

  1. 使用setDestinations(listOf(origin, dest), CustomRoutesOptions(routeToken))是否是兑换固定起点路线令牌的正确方式?或者说,当使用routeToken时,是否不应该手动传入起点途经点(即起点由当前GPS位置自动提供)?
  2. routeToken是否能保证导航器严格遵循令牌对应的路径,还是说它只是一个参考性的导航规划,SDK会根据实时交通、道路事件或轻微的起点位置差异自动调整路线?
  3. 如果我希望强制使用我选择的备选路线(而非默认最快路线),在Navigation SDK中支持的标准实现方式是什么?

希望能得到相关的使用指导,或是官方文档中关于如何让导航器严格遵循选定路线(而非重新选择最快路线)的说明。


内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 07:29:31