如何使用Java中的改造从Google附近的搜索API检索图像网址

时间:2019-06-19 06:09:08

标签: java android google-places-api

我正在尝试通过Retrofit从Google Places API(附近搜索)在Android的recyclerview中检索图像。如何检索图像并将毕加索的图像加载到图像持有器中

Here is the response from result

   public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.PlacesViewHolder> {

private Context ctx;
private List<Result> results;

public PlacesAdapter(Context ctx, List<Result> results) {
    this.ctx = ctx;
    this.results = results;
}

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

    LayoutInflater inflater = LayoutInflater.from(ctx);
    View view = inflater.inflate(R.layout.single_places_layout, parent, false);
    return new PlacesViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull PlacesViewHolder holder, int position) {
    Result result = results.get(position);
    holder.places_name.setText(result.getName());
}

@Override
public int getItemCount() {
    return results.size();
}
public class PlacesViewHolder extends RecyclerView.ViewHolder {
    ImageView places_picture;
    TextView places_name;
    public PlacesViewHolder(@NonNull View itemView) {
        super(itemView);
        places_picture = itemView.findViewById(R.id.places_pic);
        places_name = itemView.findViewById(R.id.places_name); }
}}
















public class PlacesAPI {
private static final String baseUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/";
public static PlacesService placesService = null ;
//Using singleton pattern for creating an instance of object only once
public static PlacesService getPlacesService(){
    if(placesService == null){
        Retrofit retrofit= new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        placesService = retrofit.create(PlacesService.class);}
    return placesService;}



public interface  PlacesService{
    @GET("json")
    Call<NearbyList> getResult( @QueryMap (encoded = true)Map<String, String> options );
}}





















      public class NearbyActivity extends AppCompatActivity {

private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private RecyclerView recyclerView ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nearby);
    Objects.requireNonNull(getSupportActionBar()).hide();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    recyclerView = findViewById(R.id.nearby_places_of_interest_recycler);
    recyclerView.setLayoutManager(new GridLayoutManager(this , 2));

    getData();
}




private void getData() {

    mAuth = FirebaseAuth.getInstance();
    String user_id = mAuth.getCurrentUser().getUid();

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

    mDatabase.child(user_id).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String lat = dataSnapshot.child("lat").getValue().toString();
            String lng = dataSnapshot.child("lng").getValue().toString();

            String location = lat + "," + lng;


            Map<String, String> data = new HashMap<>();
            data.put("key", "AIzaSyB2hud79JTSGmcEFxqB_ErdkYBR4FgIUMU");
            data.put("type", "cafe");
            data.put("radius", "2000");
            data.put("location", location);


            Call<NearbyList> nearbyListCall = PlacesAPI.getPlacesService().getResult(data);

            nearbyListCall.enqueue(new Callback<NearbyList>() {
                @Override
                public void onResponse(Call<NearbyList> call, Response<NearbyList> response) {

                    NearbyList list = response.body();
                    assert list != null;
                    recyclerView.setAdapter(new PlacesAdapter(NearbyActivity.this , list.getResults()));
                    Toast.makeText(NearbyActivity.this, "Success", Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onFailure(Call<NearbyList> call, Throwable t) {

                    Toast.makeText(NearbyActivity.this, "Error", Toast.LENGTH_SHORT).show();

                }
            });

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}}

在这里,我想在适配器中检索图像并使用picasso在imageview中显示它。如果有人可以帮助,那就太好了。我已经浏览了互联网和youtube上的所有内容,但找不到解决方法

1 个答案:

答案 0 :(得分:0)

您可以从附近搜索请求返回的每个位置的结果中,从照片阵列中获取照片参考,然后使用Place Photos获取图像。

也请删除或混淆代码示例和屏幕截图中的API密钥,因为它不应公开。

希望这会有所帮助。