【问题标题】:image not loading from URL in custom infoWindow using Picasso image loading library in android使用 android 中的 Picasso 图像加载库从自定义 infoWindow 中的 URL 加载图像
【发布时间】:2014-07-02 10:13:34
【问题描述】:

当用户单击标记时,我正在尝试从自定义 infoWindow 中的 URL 加载图像。我能够加载其他细节,但图像没有加载它。我正在使用毕加索图书馆为我做这件事。我搜索了许多相关主题并查看了解决方案,但无法理解应用哪种解决方案来解决我的问题。我正在使用 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

【问题讨论】:

标签: android android-maps


【解决方案1】:

你可以使用 Picasso Callback onsuccess 加载图像,像这样

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

并创建一个新类来处理像 MarkerCallback.java 这样的 Picasso 回调

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

【讨论】:

  • 您的 onSuccess 方法有效,但我不知道为什么。你能解释一下这不会在 getInfoWindow 中触发无限递归吗?
  • onSuccess 只调用一次:第一次点击图片时,图片下载后。他实际上所做的只是“刷新”信息窗口。
  • 谢谢,这很好用!我猜信息窗口视图不会失效.. 谷歌地图优化。应该有一种方法可以使信息窗口无效()。
  • 我一直在关注您的代码,因为我觉得我正在尝试实现同样的目标。我的问题是代码here。我似乎收到了错误,但不知道为什么:/也许既然你解决了它,你可能知道我做错了什么?
  • 我收到 com.squareup.picasso.NetworkRequestHandler$ResponseException: HTTP 504
【解决方案2】:

mMap.setInfoWindowAdapter(对象:GoogleMap.InfoWindowAdapter { 覆盖 fun getInfoContents(p0: Marker?): View? { 返回空 }

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

【讨论】:

  • edit将此信息添加到答案中-不要将其添加到cmets中。
【解决方案3】:

很棒的答案。工作完美且易于实施。

【讨论】:

    猜你喜欢
    • 2018-07-14
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2019-08-20
    • 1970-01-01
    相关资源
    最近更新 更多