2017-08-11 161 views
0

我已阅读google documentation,并且我已经从我的附近的搜索请求中提取了photoreference密钥。当我们运行谷歌地点照片API的URL,然后我们得到的图像。由于我只有一个网址,因此如何在图片视图上显示该图片网址。如何检索使用谷歌地方api附近的地方的照片?

我莫代尔类:

 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); 

      } 
     } 


    } 
} 

请注意,我将形成图像的URL通过放置照片参考, API密钥和宽度,我想知道如何在图像视图中以图像的形式显示此图像url

回答

1

您可以使用Picasso;

ImageView thumbNail; 

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

这里'photo_reference'是照片的网址? –

+0

显示在json响应的地方api ...哪个按键给你地点的图像 –

+0

从响应存储中获取一个字符串并在ImageView中使用毕加索加载...这就是它 –