2016-08-26 12 views
0

Google Maps Autocomplete APIコールのlocation属性に緯度と経度の値を渡したいと思いますが、RetrofitでGET呼び出しをどのように作成するかわかりません。 URLは、最終的には次のようになります。Google Maps API Retrofit GETコールを使用

https://maps.googleapis.com/maps/api/place/autocomplete/json?&types=address&input=user_input&location=37.76999,-122.44696&radius=50000&key=API_KEY 

私は現在、私の改修・インターフェースを持っている:

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

それでは、どのためのGETメソッド私が正しくセットアップすることができますgetStreetAddrResults()

また、私のデータ型は緯度/経度と半径が正しいですか?助けてくれてありがとう!

答えて

6

あなたのインターフェイスは、次のようになります。

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(); 
    } 
}); 

あなたはパラメータに値を与える必要があります。

+0

あなたが答えたとおり、私はちょうどそれを理解した。キーは、緯度と経度の値から文字列を作成することでした。 2つの別々のパラメータとしてそれらを渡そうとすると、問題が発生しました。深い答えをありがとう、私はそれを働かせた:] –

+0

うん、右;)私は助けてうれしい! –