我想将纬度和经度值传递给Google Maps Autocomplete API调用的location
属性,但我不知道如何在Retrofit中形成GET调用。 URL最终应如下所示:
https://maps.googleapis.com/maps/api/place/autocomplete/json?&types=address&input=user_input&location=37.76999,-122.44696&radius=50000&key=API_KEY
我目前在Retrofit界面中拥有的内容:
public interface GooglePlacesAutoCompleteAPI
{
String BASE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/";
String API_KEY = "mykey"; //not the actual key obviously
//This one works fine
@GET("json?&types=(cities)&key=" + API_KEY)
Call<PlacesResults> getCityResults(@Query("input") String userInput);
//This is the call that does not work
@GET("json?&types=address&key=" + API_KEY)
Call<PlacesResults> getStreetAddrResults(@Query("input") String userInput,
@Query("location") double latitude, double longitude,
@Query("radius") String radius);
}
我的错误是:java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #3) for method GooglePlacesAutoCompleteAPI.getStreetAddrResults
那我怎样才能为getStreetAddrResults()
正确设置GET方法?
另外,我的数据类型是否适合纬度/经度和半径?谢谢你的帮助!
答案 0 :(得分:7)
您的界面应如下所示:
public interface API {
String BASE_URL = "https://maps.googleapis.com";
@GET("/maps/api/place/autocomplete/json")
Call<PlacesResults> getCityResults(@Query("types") String types, @Query("input") String input, @Query("location") String location, @Query("radius") Integer radius, @Query("key") String key);
}
并像这样使用它:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
API service = retrofit.create(API.class);
service.getCityResults(types, input, location, radius, key).enqueue(new Callback<PlacesResults>() {
@Override
public void onResponse(Call<PlacesResults> call, Response<PlacesResults> response) {
PlacesResults places = response.body();
}
@Override
public void onFailure(Call<PlacesResults> call, Throwable t) {
t.printStackTrace();
}
});
当然你应该给参数赋值。