从 Android 消耗 Google 海拔 Api
Consume Google Elevations Api from Android
我想获取包含给定纬度和经度的海拔高度的 json 文件(并提供 api 键)。我看了很多书,发现 Retrofit 是 Android 中的最佳选择,但我不知道如何指定参数。
public interface ServiceApi {
//url format
//"https://maps.googleapis.com/maps/api/elevation/json?locations="
// latitude+","+longitude+"&key="+key;
@GET("")
public void getJSON(Callback<List<JsonElevation>> jsonElevationCallback);
}
我没有 Elevations API 键来测试它,但使用 Retrofit 的 documentation,类似这样的东西应该可以工作。
@GET("/maps/api/elevation/json")
public Call<List<JsonElevation>> getJSON(@Query("locations") String latAndLng,
@Query("key") String key);
然后调用它:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://maps.googleapis.com/")
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.build();
ServiceApi serviceApi = retrofit.create(ServiceApi.class);
String latAndLng = String.format("%f,%f", latitude, longitude);
Call<List<JsonElevation>> elevations = serviceApi.getJSON(latAndLng, key);
elevations.enqueue(); // for asychronous response
//or
elevations.execute(); // for synchronous response
我想获取包含给定纬度和经度的海拔高度的 json 文件(并提供 api 键)。我看了很多书,发现 Retrofit 是 Android 中的最佳选择,但我不知道如何指定参数。
public interface ServiceApi {
//url format
//"https://maps.googleapis.com/maps/api/elevation/json?locations="
// latitude+","+longitude+"&key="+key;
@GET("")
public void getJSON(Callback<List<JsonElevation>> jsonElevationCallback);
}
我没有 Elevations API 键来测试它,但使用 Retrofit 的 documentation,类似这样的东西应该可以工作。
@GET("/maps/api/elevation/json")
public Call<List<JsonElevation>> getJSON(@Query("locations") String latAndLng,
@Query("key") String key);
然后调用它:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://maps.googleapis.com/")
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.build();
ServiceApi serviceApi = retrofit.create(ServiceApi.class);
String latAndLng = String.format("%f,%f", latitude, longitude);
Call<List<JsonElevation>> elevations = serviceApi.getJSON(latAndLng, key);
elevations.enqueue(); // for asychronous response
//or
elevations.execute(); // for synchronous response