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

如何在Android中使用Google Maps Routes API gRPC?求实现方法与示例

在Android中用gRPC调用Google Maps Routes API实现路径规划

一、直接使用官方预定义的Proto文件

你不需要手动编写proto消息定义,Google已提供完整的Routes API gRPC proto集合,直接通过Gradle依赖引入即可:

  • 在Module级别的build.gradle中添加以下依赖,自动获取预编译的gRPC客户端和proto定义:
    dependencies {
        // Routes API gRPC客户端
        implementation 'com.google.maps.routing:routes-grpc-v2:1.0.0'
        // Android gRPC核心库
        implementation 'io.grpc:grpc-android:1.59.0'
        // 轻量版Protobuf适配移动端
        implementation 'io.grpc:grpc-protobuf-lite:1.59.0'
    }
    
    注意替换为Maven仓库中的最新版本号。

二、实现gRPC调用的核心步骤

  1. 创建gRPC通道
    指向Routes API的官方端点,同时配置API密钥作为请求元数据:

    // 初始化gRPC通道
    val channel = Grpc.newChannelBuilder("routes.googleapis.com:443", GoogleCredentials.create(null))
        .build()
    // 创建Routes服务的Stub实例
    val routesStub = RoutesGrpc.newStub(channel)
    
  2. 构建请求并发起异步调用
    按照ComputeRoutesRequest结构组装请求,将API密钥放入请求元数据:

    // 构建起点和终点坐标
    val origin = Waypoint.newBuilder()
        .setLocation(Location.newBuilder().setLatitude(37.7749).setLongitude(-122.4194))
        .build()
    val destination = Waypoint.newBuilder()
        .setLocation(Location.newBuilder().setLatitude(37.3382).setLongitude(-121.8863))
        .build()
    
    // 组装路径规划请求
    val request = ComputeRoutesRequest.newBuilder()
        .setOrigin(origin)
        .setDestination(destination)
        .build()
    
    // 设置API密钥元数据
    val metadata = Metadata()
    metadata.put(
        Metadata.Key.of("x-goog-api-key", Metadata.ASCII_STRING_MARSHALLER),
        "你的Google Maps API密钥"
    )
    
    // 发起异步调用并处理响应
    routesStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata))
        .computeRoutes(request, object : StreamObserver<ComputeRoutesResponse> {
            override fun onNext(response: ComputeRoutesResponse) {
                // 解析返回的路径数据,比如获取路线时长、编码折线等
                val route = response.routesList.firstOrNull()
                route?.let {
                    val duration = it.duration
                    val encodedPolyline = it.polyline.encodedPolyline
                    // 后续可将数据用于UI展示
                }
            }
    
            override fun onError(t: Throwable) {
                // 处理调用错误,如权限不足、网络异常等
                t.printStackTrace()
            }
    
            override fun onCompleted() {
                // 请求完成后关闭通道
                channel.shutdown()
            }
        })
    

三、关键注意事项

  • 确保你的Google Cloud项目已启用Routes API,且API密钥拥有该API的调用权限。
  • Android中gRPC调用不能在主线程执行,建议用Kotlin协程、线程池或RxJava包裹异步逻辑,避免ANR。
  • 若需减小APK体积,优先使用grpc-protobuf-lite而非完整版grpc-protobuf。

四、示例参考

目前Google没有专门针对Maps Routes API的Android gRPC示例,但你可以结合官方gRPC Android demo的基础框架,对照Routes API的RPC文档调整请求参数,即可完成完整的路径规划功能。

内容的提问来源于stack exchange,提问作者Hihellooo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 09:25:27