【问题标题】:How to retrieve an image url from Google nearby search API using retrofit in java如何使用 Java 中的改造从 Google 附近的搜索 API 检索图像 url
【发布时间】:2019-06-19 06:09:08
【问题描述】:

我正在尝试通过改造从 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 上的所有内容,但找不到解决此问题的方法

【问题讨论】:

  • 分享你的代码,到目前为止你做了什么。
  • Manav 如果你能分享你到目前为止所做的事情,那就太好了。一种方法是调用 api 来获取结果。仅在结果中使用放置图像 url 项来填充您的回收站视图
  • 显示您如何调用PlacesService,以及它返回的结果示例

标签: java android google-maps google-places-api


【解决方案1】:

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-10-31
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    相关资源
    最近更新 更多