Ghost_Stark Ghost_Stark - 3 months ago 77
Android Question

Google Maps API using Retrofit GET call

I want to pass in the latitude and longitude values to the

location
attribute of the Google Maps Autocomplete API call but I have no idea how to form the GET call in Retrofit. The URL should ultimately look like this:

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


What I currently have in my Retrofit interface:

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


My error is:
java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #3) for method GooglePlacesAutoCompleteAPI.getStreetAddrResults


So how can I correctly setup the GET method for
getStreetAddrResults()
?

Also, are my data types correct for latitude/longitude and radius? Thanks for any help!

Answer

Your interface should looks like this:

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

And use it like this:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

YourInterface service = retrofit.create(YourInterface.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();
    }
});

Of course you should give a value to the parameters.