【问题标题】:Android Retrofit + Google Places API (text search)Android Retrofit + Google Places API(文本搜索)
【发布时间】:2016-11-24 13:17:59
【问题描述】:

我正在尝试根据给定查询获取地点。我实现了以下接口。

 @GET("api/place/textsearch/json?key=MY_API_KEY")
Call<PlaceResponse> getNearbyPlacesByText(@Query("query") String query, @Query("location") String location, @Query("radius") int radius);

那我这样调用这个方法

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

    ApiInterface service = retrofit.create(ApiInterface.class);
 Call<PlaceResponse> callByText = service.getNearbyPlacesByText("psí" + "škola", currentLocation.getLatitude() + "," + currentLocation.getLongitude(), PROXIMITY_RADIUS);

然后我正在检索结果。

 callByText.enqueue(new Callback<PlaceResponse>() {
        @Override
        public void onResponse(Call<PlaceResponse> call, Response<PlaceResponse> response) {
            List<Place> places = response.body().getResults();
            Log.d(TAG, "Number of places received: " + places.size());
            setMarkers(type, places, googleMap, context);

        }


        @Override
        public void onFailure(Call<PlaceResponse> call, Throwable t) {

        }

    });

但是我得到了更多与给定查询不匹配的结果(我得到了 20 个位置)。我在 Postman 中尝试使用 URL https://maps.googleapis.com/maps/api/place/textsearch/json?query=psí+škola&location=49.188963,16.532183&radius=50000&key=MY_KEY

我不确定我是否在代码中正确设置了网址。

感谢您的建议 我只有 8 个位置,这是正确的数字。我不确定我是否

【问题讨论】:

    标签: android google-maps google-places-api retrofit2


    【解决方案1】:

    您可以在 Retrofit 中设置日志记录并检查正在发送的内容(例如在 Kotlin 中)。

    val logging = HttpLoggingInterceptor()
    logging.level = HttpLoggingInterceptor.Level.BODY
    val httpClient = OkHttpClient.Builder()
    httpClient.addInterceptor(logging)
    
    Retrofit.Builder()
            .baseUrl(url)
            .client(httpClient.build())
            .build()
            .create(ApiDao::class.java)
    

    【讨论】:

      猜你喜欢
      • 2018-08-08
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 2014-10-01
      相关资源
      最近更新 更多