代码之家  ›  专栏  ›  技术社区  ›  jobin

Android无法使用Google API检索附近的位置

  •  0
  • jobin  · 技术社区  · 7 年前

    我正在尝试使用谷歌地图的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生成器插件创建POJO类。但是在修改的响应方法中,POJO类中的状态变量给了我“RealestyBuffic”。(main Actudio.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:主要活动.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");
                                }
                            });
    

    Java类:

    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;
            }
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   MRah    7 年前

    前提是您实际上已经有一个API密钥, if not,,

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

    一旦您获得了API密钥,并且想要启用API和服务,您就可以始终转到API控制台并启用API和服务,

    转到 https://console.cloud.google.com/apis/dashboard?您的项目

    单击“启用API”按钮

    启用要使用的API和服务。

    您应该启用 maps sdk for android和您可能想要使用的任何其他服务。

    更新:

    如果您正在进行HTTP请求(例如@g e t),请确保您没有在API控制台中对您的API设置限制。因此,您需要转到生成的密钥并找到以下内容,并设置为“无限制”,

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

    例如,placePicker object in places api

    我正在参考开发人员网站上的PlacesSDK,

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

    但是,如果您希望发出HTTP请求,那么它将被拒绝,因为您在密钥中设置了限制。

    一旦您获得了API密钥,并且想要启用API和服务,就可以随时转到API控制台并启用API和服务,

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

    单击“启用API”按钮

    Enable APIS and Services

    启用要使用的API和服务。

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

    更新:

    如果您正在进行HTTP请求(例如@g e t),请确保您没有在API控制台中对您的API设置限制。因此,您需要转到生成的密钥并找到以下内容,然后设置为“无限制”,

    Google API Console

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

    例如,placePicker对象在placesAPI中 .

    我在开发者网站上引用PlacesSDK,

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

    但是,如果您希望发出HTTP请求,那么它将被拒绝,因为您在密钥中设置了限制。