简体   繁体   English

Android无法使用Google API检索附近的地方

[英]Android unable to retrieve nearby places using google api

I am trying to get the nearby places of the location I send by using the google map's api. 我正在尝试使用Google地图的api获取我发送的位置的附近地点。 Below is the 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 created a POJO class using DTO Generator plugin.But in the response method of the retrofit the status variable in the POJO class gives me 'REQUEST_DENIED'.(Main Activity.java) 以下是网址( https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=1500&type=restaurant&keyword=cruise&key=YOUR_API_KEY)。我使用DTO Generator创建了POJO类插件。但是在改造的响应方法中,POJO类中的状态变量给了我'REQUEST_DENIED'(Main Activity.java)

Log.e("Res",response.body().getStatus()); Log.e( “RES”,response.body()的getStatus());

Am I using retrofit the right way? 我使用正确的改造方法吗?

RetrofitClient.java: 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: 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: 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 class: (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;
        }
    }
}

Provided that you actually have an API Key already, if not, 假设您实际上已经拥有一个API密钥(如果没有),

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

Once you get your api key and you want to enable api and services, you can always go to your api console and enable apis and services, 获得api密钥并想要启用api和服务后,您随时可以转到api控制台并启用api和服务,

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

Click in the Enable Apis Button 单击启用Apis按钮

启用APIS和服务

Enable the APIs and services that you want to use. 启用要使用的API和服务。

You should enable Maps SDK for Android and any other services you might want to use. 您应启用Android版Maps SDK和您可能想使用的任何其他服务。

UPDATE: 更新:

If you are making an Http request (eg @GET), please make sure that you don't place a restriction on your API in the api console. 如果您正在发出Http请求(例如@GET),请确保您未在​​api控制台中对API施加限制。 So you need to go to your generated key and find the following and set to no restrictions, 因此,您需要转到生成的密钥并找到以下内容,并将其设置为无限制,

Google API控制台

What will happen if you select Android only? 如果仅选择Android,会发生什么? You'll be able to use any objects and methods in the selected SDK (eg places/maps). 您将能够使用所选SDK中的任何对象和方法(例如,位置/地图)。

For example, PlacePicker object in places API . 例如,places API中的PlacePicker对象

I am giving reference to places sdk from developers website, 我正在参考开发者网站上的place sdk,

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

But if you want make HTTP request then it will get denied as you have restrictions placed in your key. 但是,如果您要发出HTTP请求,则由于密钥中有限制,它将被拒绝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Google Places API检索附近地点的照片? - How to retrieve the photo of the nearby places using google places api? 在Android上使用Google Places API获取附近地点的列表 - get list of nearby places using google places API on android Android:使用Google Places API查找附近的地点 - Android: Finding nearby places with Google Places API 使用Google Places API查找附近的地方 - find nearby places using Google Places api 如何使用 Google Place API 创建附近的地方 android 应用程序? - How to create an nearby places android app using Google Place API? Android:如何使用Google Places API(自动填充)搜索附近的酒店? - Android: How to search for nearby hotels using the Google Places API (Autocomplete)? 我在使用google map附近的地方API。 我的代码仅显示附近的地点 - I am using google map nearby places API in android. My code only shows the near by places 使用谷歌搜索特定的附近地方为Android安置api web服务 - Search specific nearby places using google places api web services for android 使用Android中的Google Maps API显示当前位置和附近地点以及两个地点之间的路线 - Show Current Location and Nearby Places and Route between two places using Google Maps API in Android Google Places API获取Android中附近地点的距离 - Google places api get distance of nearby places in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM