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

Android调用Google Elevations API获取海拔的Retrofit参数配置问题

嘿,我来帮你搞定这个Retrofit调用谷歌海拔API的问题!你现在的接口缺了关键的路径和参数配置,我一步步给你梳理清楚:

修正你的Retrofit ServiceApi接口

首先,谷歌海拔API的基础URL是https://maps.googleapis.com/maps/api/elevation/,所以我们需要在@GET注解里填相对路径"json",然后用@Query注解来传递动态的经纬度和API密钥参数。

修正后的ServiceApi代码(适配Retrofit 2.x,推荐版本)

public interface ServiceApi {
    // 基础URL对应 "https://maps.googleapis.com/maps/api/elevation/"
    @GET("json")
    Call<ElevationResponse> getElevation(
            @Query("locations") String coordinates, // 格式:"纬度,经度",比如"39.9042,116.4074"
            @Query("key") String apiKey
    );
}

关键细节说明

  • 把原来的void返回类型改成Call<ElevationResponse>,这是Retrofit 2.x的标准用法(如果还在使用Retrofit 1.x,可以保留Callback参数,但强烈建议升级到2.x版本)。
  • @Query("locations")用来传递经纬度拼接字符串:你需要把纬度和经度用逗号连起来,比如"39.9042,116.4074",直接传给这个参数即可。
  • @Query("key")用来传递你的谷歌地图API密钥:注意要确保这个密钥已经在谷歌云控制台启用了海拔API的权限,否则会返回权限错误。

配套的实体类与调用示例

1. 匹配谷歌返回结构的实体类

谷歌海拔API返回的JSON结构大概是这样的:

{
  "results": [
    {
      "elevation": 10.0,
      "location": {
        "lat": 39.9042,
        "lng": 116.4074
      },
      "resolution": 152.70323181152344
    }
  ],
  "status": "OK"
}

所以我们需要创建两个实体类来适配:

// 包裹整个返回的根对象
public class ElevationResponse {
    private List<ElevationResult> results;
    private String status;

    // 生成getter和setter方法
}

// 单个海拔结果对象
public class ElevationResult {
    private double elevation;
    private Location location;
    private double resolution;

    // 生成getter和setter方法

    // 内部类:经纬度对象
    public static class Location {
        private double lat;
        private double lng;

        // 生成getter和setter方法
    }
}

2. 初始化Retrofit并发起请求

// 初始化Retrofit实例
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://maps.googleapis.com/maps/api/elevation/")
        .addConverterFactory(GsonConverterFactory.create()) // 需要添加Gson依赖来解析JSON
        .build();

// 创建ServiceApi实例
ServiceApi serviceApi = retrofit.create(ServiceApi.class);

// 发起异步请求
Call<ElevationResponse> call = serviceApi.getElevation("39.9042,116.4074", "你的谷歌API密钥");
call.enqueue(new Callback<ElevationResponse>() {
    @Override
    public void onResponse(Call<ElevationResponse> call, Response<ElevationResponse> response) {
        if (response.isSuccessful() && response.body() != null) {
            ElevationResponse elevationResponse = response.body();
            if ("OK".equals(elevationResponse.getStatus())) {
                // 获取海拔数据
                double elevation = elevationResponse.getResults().get(0).getElevation();
                // 处理数据逻辑
            } else {
                // 处理API返回的错误状态,比如INVALID_REQUEST、OVER_QUERY_LIMIT等
            }
        } else {
            // 处理请求失败(比如404、500等HTTP错误)
        }
    }

    @Override
    public void onFailure(Call<ElevationResponse> call, Throwable t) {
        // 处理网络错误,比如无网络连接、超时等
        t.printStackTrace();
    }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:50:22