图像没有从自定义infoWindow中的URL加载使用Android中的Picasso图像加载库

时间:2014-07-02 10:13:34

标签: android android-maps

当用户点击标记时,我正尝试从自定义infoWindow中的URL加载图像。我能够加载其他细节,但图像没有加载它。我正在使用Picasso库为我做这件事。我搜索了许多相关主题并查看了解决方案,但无法理解应用哪种解决方案来解决我的问题。我正在使用Web服务调用来获取所需的数据。

这是我的代码:

  @Override
        public View getInfoContents(Marker arg0) {

            //marker.showInfoWindow();
            return null;
        }

        @Override
        public View getInfoWindow(Marker arg0) 
        {

            View v = getLayoutInflater().inflate(R.layout.map_infowindow, null);


             for(int i=0 ; i<lstMain.size();i++)
                {
                    Pojo b=lstMain.get(i);
                    double slat= Double.parseDouble(b.getLatitude());
                    double slng= Double.parseDouble(b.getLongitude());

                    double lat= Math.round(slat*100000.0)/100000.0;
                    double lng= Math.round(slng*100000.0)/100000.0;

                    String s1=String.valueOf(lat);
                    String s2=String.valueOf(lng);

                    Log.v("comparing latilongi pojo===>", lat+" , "+lng);
                    Log.v("comparing latilongi marker===>", Clickedlatitude+" , "+Clickedlongitude);

                    if(s1.equals(String.valueOf(Clickedlatitude)) && s2.equals(String.valueOf(Clickedlongitude)))
                    {
                        txtDname=(TextView)v.findViewById(R.id.infoDoctorName);
                        txtDspe=(TextView)v.findViewById(R.id.infoDoctorSpe);
                        txtDaddress=(TextView)v.findViewById(R.id.infoDoctorAddress);
                        imgUrl=(ImageView)v.findViewById(R.id.infoDoctorImage);
                        String url=b.getPhotoURL();

                        txtDname.setText(b.getDoctorName());
                        txtDspe.setText(b.getSpecialization());
                        txtDaddress.setText(b.getAddress1()+" , "+b.getAddress2());


                        Picasso.with(MapActivity.this)
                        .load(url)
                        .placeholder(R.drawable.ic_launcher)
                        .error(R.drawable.ic_launcher).resize(50, 50)
                        .into(imgUrl);


                    }

                }
            return v;
        }

我已经看过这些解决方案,但不明白究竟是什么能解决我的问题:

Why Custom InfoWindow of google map v2 ,Not load url Image?

Android Google maps APIv2 InfoWindow and Markers

Add an Image from url into custom InfoWindow google maps v2

3 个答案:

答案 0 :(得分:26)

您可以使用Picasso Callback onccess成功加载图片,就像这样

if (image != null) {
   Picasso.with(getApplicationContext())
          .load(image)
          .placeholder(R.drawable.ic_launcher)
          .into(i, new MarkerCallback(marker));
}

并创建一个新类来处理Picasso Callback,就像这个MarkerCallback.java

public class MarkerCallback implements Callback {
   Marker marker=null;

   MarkerCallback(Marker marker) {
     this.marker=marker;
   }

   @Override
   public void onError() {
     Log.e(getClass().getSimpleName(), "Error loading thumbnail!");
   }

   @Override
   public void onSuccess() {
     if (marker != null && marker.isInfoWindowShown()) {
       marker.hideInfoWindow();
       marker.showInfoWindow();
     }
   }
}

答案 1 :(得分:1)

mMap.setInfoWindowAdapter(object:GoogleMap.InfoWindowAdapter {             重写fun getInfoContents(p0:Marker?):视图? {                 返回null             }

        override fun getInfoWindow(p0: Marker?): View? {

            if (p0!!.tag != null) {
                val view = LayoutInflater.from(this@ProviderMainActivity).inflate(R.layout.infowindow_client, null)
                val text = view.findViewById<TextView>(R.id.TXT_NAME)
                val TXT_location = view.findViewById<TextView>(R.id.TXT_location)
                val proimg = view.findViewById<ImageView>(R.id.IMG)
                val datares = p0!!.tag as ResponseDataItem

                Glide.with(this@ProviderMainActivity).load(datares.user!!.profileImageWithURL)
                    .apply(RequestOptions.circleCropTransform().override(50,50).placeholder(R.drawable.placeholder_profile))
                    .listener(object :RequestListener<Drawable>{
                        override fun onLoadFailed(
                            e: GlideException?,
                            model: Any?,
                            target: Target<Drawable>?,
                            isFirstResource: Boolean
                        ): Boolean {
                            return true
                        }

                        override fun onResourceReady(
                            resource: Drawable?,
                            model: Any?,
                            target: Target<Drawable>?,
                            dataSource: DataSource?,
                            isFirstResource: Boolean
                        ): Boolean {
                            Klog.d("## FIRST RESOURCE-","-".plus(isFirstResource))
                            Klog.d("## FIRST DATA SOURCE-","-".plus(dataSource?.name))
                            proimg.setImageDrawable(resource)
                            if (dataSource?.name.equals("DATA_DISK_CACHE")) {
                                p0.showInfoWindow()
                            }
                            return true
                        }

                    }).into(proimg)
                text.setText(datares.user!!.fullName)
                var loc = datares.location!!.text!!.split(",").map { it.trim() }
                TXT_location.setText(loc.get(0))

                return view
            } else {
                return null
            }
        }
    })

答案 2 :(得分:0)

一流的答案。完美且易于实施。