【问题标题】:Android unable to retrieve nearby places using google apiAndroid无法使用google api检索附近的地方
【发布时间】:2018-12-27 18:31:03
【问题描述】:

我正在尝试使用谷歌地图的 api 获取我发送位置的附近地点。下面是 url(https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=1500&type=restaurant&keyword=cruise&key=YOUR_API_KEY).I 使用 DTO Generator 插件创建了一个 POJO 类。但是在改造的响应方法中,POJO 类中的状态变量给了我 'REQUEST_DENIED'。(Main Activity.java)

Log.e("Res",response.body().getStatus());

我是否以正确的方式使用改造?

RetrofitClient.java:

public class RetrofitClient {
    public static Retrofit retrofit=null;
     static final String baseurl="https://maps.googleapis.com/maps/api/place/nearbysearch/";
    public static Retrofit getClient()
    {
        if (retrofit==null)
        {
            retrofit=new Retrofit.Builder().baseUrl(baseurl).addConverterFactory(GsonConverterFactory.create()).build();
        }
        return retrofit;
    }
}

NearbyApi.java:

public interface NearbyApi {
    @GET("json?")
    Call<NearbyPlaces> getDetails(@Query("location") String loc,
                                  @Query("radius") String radius,
                                  @Query("type") String type,
                                  @Query("keyword") String keyword,@Query("key") String key);

}

MainActivity.java:

String loc="-33.8670522,151.1957362";
    String radius="1500";
    String type="restaurant";
    String keyword="cruise";
    String key="****";
    NearbyApi nearbyApi=RetrofitClient.getClient().create(NearbyApi.class);
                        Call<NearbyPlaces> call=nearbyApi.getDetails(loc,radius,type,keyword,key);
                        call.enqueue(new Callback<NearbyPlaces>() {
                            @Override
                            public void onResponse(Call<NearbyPlaces> call, Response<NearbyPlaces> response) {

    //                                Log.e("Res",response.body().getStatus());
                                Log.e("Res",response.body().getStatus());
                                }

                            @Override
                            public void onFailure(Call<NearbyPlaces> call, Throwable t) {
                                Log.e("Response","Failure");
                            }
                        });

(NearbyPlaces.java)POJO 类:

public class NearbyPlaces {

    @Expose
    @SerializedName("status")
    private String status;
    @Expose
    @SerializedName("results")
    private List<Results> results;
    @Expose
    @SerializedName("html_attributions")
    private List<String> html_attributions;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<Results> getResults() {
        return results;
    }

    public void setResults(List<Results> results) {
        this.results = results;
    }

    public List<String> getHtml_attributions() {
        return html_attributions;
    }

    public void setHtml_attributions(List<String> html_attributions) {
        this.html_attributions = html_attributions;
    }

    public static class Results {
        @Expose
        @SerializedName("vicinity")
        private String vicinity;
        @Expose
        @SerializedName("types")
        private List<String> types;
        @Expose
        @SerializedName("scope")
        private String scope;
        @Expose
        @SerializedName("reference")
        private String reference;
        @Expose
        @SerializedName("rating")
        private int rating;
        @Expose
        @SerializedName("plus_code")
        private Plus_code plus_code;
        @Expose
        @SerializedName("place_id")
        private String place_id;
        @Expose
        @SerializedName("photos")
        private List<Photos> photos;
        @Expose
        @SerializedName("opening_hours")
        private Opening_hours opening_hours;
        @Expose
        @SerializedName("name")
        private String name;
        @Expose
        @SerializedName("id")
        private String id;
        @Expose
        @SerializedName("icon")
        private String icon;
        @Expose
        @SerializedName("geometry")
        private Geometry geometry;

        public String getVicinity() {
            return vicinity;
        }

        public void setVicinity(String vicinity) {
            this.vicinity = vicinity;
        }

        public List<String> getTypes() {
            return types;
        }

        public void setTypes(List<String> types) {
            this.types = types;
        }

        public String getScope() {
            return scope;
        }

        public void setScope(String scope) {
            this.scope = scope;
        }

        public String getReference() {
            return reference;
        }

        public void setReference(String reference) {
            this.reference = reference;
        }

        public int getRating() {
            return rating;
        }

        public void setRating(int rating) {
            this.rating = rating;
        }

        public Plus_code getPlus_code() {
            return plus_code;
        }

        public void setPlus_code(Plus_code plus_code) {
            this.plus_code = plus_code;
        }

        public String getPlace_id() {
            return place_id;
        }

        public void setPlace_id(String place_id) {
            this.place_id = place_id;
        }

        public List<Photos> getPhotos() {
            return photos;
        }

        public void setPhotos(List<Photos> photos) {
            this.photos = photos;
        }

        public Opening_hours getOpening_hours() {
            return opening_hours;
        }

        public void setOpening_hours(Opening_hours opening_hours) {
            this.opening_hours = opening_hours;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public Geometry getGeometry() {
            return geometry;
        }

        public void setGeometry(Geometry geometry) {
            this.geometry = geometry;
        }
    }

    public static class Plus_code {
        @Expose
        @SerializedName("global_code")
        private String global_code;
        @Expose
        @SerializedName("compound_code")
        private String compound_code;

        public String getGlobal_code() {
            return global_code;
        }

        public void setGlobal_code(String global_code) {
            this.global_code = global_code;
        }

        public String getCompound_code() {
            return compound_code;
        }

        public void setCompound_code(String compound_code) {
            this.compound_code = compound_code;
        }
    }

    public static class Photos {
        @Expose
        @SerializedName("width")
        private int width;
        @Expose
        @SerializedName("photo_reference")
        private String photo_reference;
        @Expose
        @SerializedName("html_attributions")
        private List<String> html_attributions;
        @Expose
        @SerializedName("height")
        private int height;

        public int getWidth() {
            return width;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public String getPhoto_reference() {
            return photo_reference;
        }

        public void setPhoto_reference(String photo_reference) {
            this.photo_reference = photo_reference;
        }

        public List<String> getHtml_attributions() {
            return html_attributions;
        }

        public void setHtml_attributions(List<String> html_attributions) {
            this.html_attributions = html_attributions;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }
    }

    public static class Opening_hours {
        @Expose
        @SerializedName("open_now")
        private boolean open_now;

        public boolean getOpen_now() {
            return open_now;
        }

        public void setOpen_now(boolean open_now) {
            this.open_now = open_now;
        }
    }

    public static class Geometry {
        @Expose
        @SerializedName("viewport")
        private Viewport viewport;
        @Expose
        @SerializedName("location")
        private Location location;

        public Viewport getViewport() {
            return viewport;
        }

        public void setViewport(Viewport viewport) {
            this.viewport = viewport;
        }

        public Location getLocation() {
            return location;
        }

        public void setLocation(Location location) {
            this.location = location;
        }
    }

    public static class Viewport {
        @Expose
        @SerializedName("southwest")
        private Southwest southwest;
        @Expose
        @SerializedName("northeast")
        private Northeast northeast;

        public Southwest getSouthwest() {
            return southwest;
        }

        public void setSouthwest(Southwest southwest) {
            this.southwest = southwest;
        }

        public Northeast getNortheast() {
            return northeast;
        }

        public void setNortheast(Northeast northeast) {
            this.northeast = northeast;
        }
    }

    public static class Southwest {
        @Expose
        @SerializedName("lng")
        private double lng;
        @Expose
        @SerializedName("lat")
        private double lat;

        public double getLng() {
            return lng;
        }

        public void setLng(double lng) {
            this.lng = lng;
        }

        public double getLat() {
            return lat;
        }

        public void setLat(double lat) {
            this.lat = lat;
        }
    }

    public static class Northeast {
        @Expose
        @SerializedName("lng")
        private double lng;
        @Expose
        @SerializedName("lat")
        private double lat;

        public double getLng() {
            return lng;
        }

        public void setLng(double lng) {
            this.lng = lng;
        }

        public double getLat() {
            return lat;
        }

        public void setLat(double lat) {
            this.lat = lat;
        }
    }

    public static class Location {
        @Expose
        @SerializedName("lng")
        private double lng;
        @Expose
        @SerializedName("lat")
        private double lat;

        public double getLng() {
            return lng;
        }

        public void setLng(double lng) {
            this.lng = lng;
        }

        public double getLat() {
            return lat;
        }

        public void setLat(double lat) {
            this.lat = lat;
        }
    }
}

【问题讨论】:

  • 这是因为您的权限被 google apis 拒绝。您需要在开始使用之前启用该 api。
  • Am I using retrofit the right way? 不是真的,您应该只创建一次 API,然后再使用它。
  • 你能在代码中纠正我吗?

标签: android retrofit


【解决方案1】:

前提是您实际上已经拥有 API 密钥,如果没有,

https://developers.google.com/places/web-service/get-api-key

获得 api 密钥并想要启用 api 和服务后,您可以随时前往 api 控制台并启用 api 和服务,

转到https://console.cloud.google.com/apis/dashboard?YOUR_PROJECT

单击启用 API 按钮

启用您要使用的 API 和服务。

您应该启用 Maps SDK for Android 以及您可能想要使用的任何其他服务。

更新:

如果您正在发出 Http 请求(例如@GET),请确保您没有在 api 控制台中对您的 API 施加限制。所以你需要去你生成的密钥,找到以下内容并设置为无限制,

如果您只选择 Android 会发生什么? 您将能够使用所选 SDK 中的任何对象和方法(例如地点/地图)。

例如,地点 API 中的 PlacePicker 对象

我正在参考开发者网站上的 places sdk,

https://developers.google.com/places/android-sdk/intro

但是,如果您想发出 HTTP 请求,那么它将被拒绝,因为您的密钥中有限制。

【讨论】:

  • 我启用了places sdk,因为附近的地方都在它之下
  • API 请求错误平均延迟(毫秒) Places SDK for Android 0 0 - 详情 Places API for Web 9 8 - 详情 Maps SDK for Android 83 0 - 详情 Maps JavaScript API 0 0 - 详情
  • 以上是启用的api。还有什么我错过的
  • 去你的sdk看看你是否有任何应用限制。由于您正在进行 get 调用,这意味着您实际上是在发出 http 请求。所以你需要从 api 中移除限制(如果有的话)
  • 我只限制了安卓应用
猜你喜欢
  • 1970-01-01
  • 2019-10-31
  • 2018-06-20
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 2016-03-05
相关资源
最近更新 更多