如何使用google places api检索附近地方的照片?

时间:2017-08-11 11:35:11

标签: android google-places-api google-play-developer-api

我已阅读google documentation,我已从附近的搜索请求中获取了photoreference密钥。当我们运行google place照片API的网址时,我们会获得图片。由于我只有一个URL,如何在图像视图上显示该图像URL。

我的模态课程:

      public class PlacesResponse {

        public class Root implements Serializable {

            @SerializedName("results")
            public ArrayList<CustomA> customA = new ArrayList<>();
            @SerializedName("status")
            public String status;
        }

        public class CustomA implements Serializable {


            @SerializedName("geometry")
            public Geometry geometry;
            @SerializedName("vicinity")
            public String vicinity;
            @SerializedName("name")
            public String name;
            @SerializedName("photos")
            public ArrayList<Photos> photos = new ArrayList<>();
            @SerializedName("place_id")
            public String place_id;

        }

        public class Photos implements Serializable{

            @SerializedName("photo_reference")
            public String photo_reference;

        }

        public class Geometry implements Serializable{

            @SerializedName("location")
            public LocationA locationA;

        }

        public class LocationA implements Serializable {

            @SerializedName("lat")
            public String lat;
            @SerializedName("lng")
            public String lng;


        }


    }

附近地点搜索请求代码

private void fetchStores(String placeType) {

        Call<PlacesResponse.Root> call = apiService.doPlaces(latLngString,radius,placeType, ApiClient.GOOGLE_PLACE_API_KEY);
        call.enqueue(new Callback<Root>() {
            @Override
            public void onResponse(Call<PlacesResponse.Root> call, Response<PlacesResponse.Root> response) {
                PlacesResponse.Root root = (Root) response.body();


                if (response.isSuccessful()) {

                    if (root.status.equals("OK")) {

                        results = root.customA;

                        storeModels = new ArrayList<>();
                        distnaceModels = new ArrayList<>();

                        Log.i(TAG,"fetch stores");

                        for (int i = 0; i < results.size(); i++) {


                            PlacesResponse.CustomA info = (CustomA) results.get(i);

                        String place_id = results.get(i).place_id;
          // this gives me the photo reference for the request

                        String photo_reference = results.get(i).photos.get(0).photo_reference;

                            fetchDistance(info);
                         //   fetchPlace_details(place_id);


                        }


                    } else {
                        Toast.makeText(getApplicationContext(), "No matches found near you", Toast.LENGTH_SHORT).show();
                    }

                } else if (response.code() != 200) {
                    Toast.makeText(getApplicationContext(), "Error " + response.code() + " found.", Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onFailure(Call call, Throwable t) {
                // Log error here since request failed
                call.cancel();
            }
        });


    }

适配器类

public class Rv_adapter extends RecyclerView.Adapter<MyViewHolder> {

    private ArrayList<PlacesResponse.CustomA> stLstStores;
    private ArrayList<Modal> storeModels;

    private static final int TYPE_HEAD=0;
    private static final int TYPE_LIST=1;

    public Rv_adapter(ArrayList<PlacesResponse.CustomA> stLstStores, ArrayList<Modal> storeModels) {

        this.stLstStores = stLstStores ;
        this.storeModels = storeModels;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        if(viewType==TYPE_LIST) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recyclerview_listitem, parent, false);

            return new MyViewHolder(itemView,viewType);
        }
        else if(viewType==TYPE_HEAD)
        {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recyclerview_header, parent, false);

            return new MyViewHolder(itemView,viewType);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {

        if(position==0){
            return TYPE_HEAD;}
        else{
            return TYPE_LIST;
        }

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {

        Log.i("adapter_posn",holder.getAdapterPosition()+"");

        Log.i("view type",holder.view_type+"");
            if(holder.view_type == TYPE_LIST) {

                holder.res_name.setText(stLstStores.get(holder.getAdapterPosition()-1).name);

                //                holder.res_address.setText(storeModels.get(holder.getAdapterPosition()-1).address);
//                holder.res_phone.setText(storeModels.get(holder.getAdapterPosition()-1).phone_no);
//                holder.res_rating.setText(String.valueOf(storeModels.get(holder.getAdapterPosition()-1).rating));
//                holder.res_distance.setText(String.valueOf(storeModels.get(holder.getAdapterPosition()-1).distance));
            }
            else if (holder.view_type == TYPE_HEAD)
            {
                holder.current_location.setText(String.valueOf(storeModels.get(holder.getAdapterPosition()).current_location));
            }
    }

    @Override
    public int getItemCount() {

        return  stLstStores.size()+1;

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView res_name;
        TextView res_rating;
        TextView res_address;
        TextView res_phone;
        TextView res_distance;
        TextView current_location;
        ImageView res_image;
        int view_type;

        public MyViewHolder(View itemView,int viewType) {
            super(itemView);

            if(viewType == TYPE_LIST) {

                view_type=1;
                this.res_name = (TextView) itemView.findViewById(R.id.name);
                this.res_rating = (TextView) itemView.findViewById(R.id.rating);
                this.res_address = (TextView) itemView.findViewById(R.id.address);
                this.res_phone = (TextView) itemView.findViewById(R.id.phone);
                this.res_distance = (TextView) itemView.findViewById(R.id.distance);
                this.res_image = (ImageView) itemView.findViewById(R.id.res_image);
            }
          else  if(viewType == TYPE_HEAD){
                view_type = 0;
                this.current_location = (TextView) itemView.findViewById(R.id.location_tv);

            }
        }


    }
}

请注意我会通过放置照片参考,API密钥和宽度来形成图片的网址,我想知道如何在图片视图中以图片的形式显示此图片网址 < / p>

1 个答案:

答案 0 :(得分:0)

你可以使用Picasso;

 ImageView thumbNail;

Picasso.with(MainActivity.this).load(photo_reference).noFade().into(thumbNail);