毕加索图像未在自定义信息窗口中加载为什么?

picasso image not loading in custom infoWindow why?

我正在尝试以编程方式布局自定义信息窗口。我想使用 Picasso 加载 streetView 预览图像,但图像没有显示,知道为什么吗?

private View prepareInfoView(Marker marker){
    //prepare InfoView programmatically
    LinearLayout infoView = new LinearLayout(EarthquakeActivity.this);
    LinearLayout.LayoutParams infoViewParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    infoView.setOrientation(LinearLayout.VERTICAL);
    // attach the above layout to the infoView
    infoView.setLayoutParams(infoViewParams);

    //create street view preview @ top
    ImageView streetViewPreviewIV = new ImageView(EarthquakeActivity.this);
    // this scales the image to match parents WIDTH?, but retain image's height??
    LinearLayout.LayoutParams streetViewImageViewParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    streetViewPreviewIV.setLayoutParams(streetViewImageViewParams);

    String imageURL = "https://maps.googleapis.com/maps/api/streetview?size=200x200&location=";
    String markerLongitude = Double.toString(marker.getPosition().longitude);
    String markerLatitude = Double.toString(marker.getPosition().latitude);
    imageURL += markerLatitude + "," + markerLongitude + "&fov=120&heading=0&pitch=0";
    Log.wtf("prepareInfoView", imageURL);

    Picasso.with(this).load(imageURL).into(streetViewPreviewIV);
    infoView.addView(streetViewPreviewIV);

我试过使用和不使用 api 键附加 url。

它在没有密钥的情况下点击几下确实有效,但从那以后,有或没有。是因为获取它太慢所以 Android 放弃并在没有它的情况下加载信息 window 吗? class 有最好的方法吗?

另一个图像加载库会更好吗? Google的凌空抽射?

还有

LinearLayout.LayoutParams

我希望图像在信息 windows 的宽度上伸展,即 match_parent,并垂直缩放以保持原始纵横比,我该怎么做?

这是我的回答

在 commonsWare new class 我添加了这个标志:

@Override
public void onSuccess() {
    Log.i(TAG, "image got, should rebuild window");
    if (marker != null && marker.isInfoWindowShown()) {
        Log.i(TAG, "conditions met, redrawing window");
        marker.setTag(new Boolean("True"));
        marker.showInfoWindow();
    }
}

并且在 prepareInfoView 中,我测试了是否缺少标志。

    if (marker.getTag() == null ) {
        Log.i("prepareInfoView", "fetching image");
        Picasso.with(this).load(imageURL).fetch(new MarkerCallback(marker));
    }
    else {
        Log.wtf("prepareInfoView", "building info window");

狂欢开始! :)

Is the because it's too slow fetching it so Android gives up and loads the info window without it?

Picasso 异步加载,除非图像被缓存。 Maps V2 的工作方式是将 View 和 return 转换为 Bitmap,这就是渲染的内容。因此,您在 Picasso 和 Maps V2 之间存在竞争条件(图像是否在 Bitmap 创建之前加载?),因此不确定是否有任何给定信息 window将工作。

您可以在 Picasso 加载图像后在 Marker 上调用 showInfoWindow(),这样您就可以从 Picasso 的缓存中填充 ImageViewshowInfoWindow(),调用 Marker,触发 Maps V2 重新生成信息 window。

例如,您可以将现有的 into() 调用更改为 into(streetViewPreviewIV, new MarkerCallback(marker)),并使用 MarkerCallback,例如:

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

Would another image loading library work better? Google's volley?

他们都会遇到同样的问题。

对我有用的是:

public class MarkerCallback implements Callback {
    Marker marker=null;
    String URL;
    ImageView userPhoto;


    MarkerCallback(Marker marker, String URL, ImageView userPhoto) {
        this.marker=marker;
        this.URL = URL;
        this.userPhoto = userPhoto;
    }

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

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

            Picasso.with(getActivity())
                    .load(URL)                        
                    .into(userPhoto);

            marker.showInfoWindow();
        }
    }
}

我只知道, Picasso 异步加载图像,因此当标记通过内部调用方法 getInfoContentsgetInfoWindow 方法单击后显示其信息 window 时, 到这个时候,如果图片还没有被 Picasso 下载或缓存,那么它就不会显示在 infoWindow 上。 Picasso 尝试在下载时将图像加载到 infoWindow 的图像视图中,但是根据 Google maps V2,infoWindows 一旦加载,可以' 被操纵,所以图像不会在 UI 上显示更新。 但是 infowindow 视图实际上已经更新但无法显示限制,所以如果你只是隐藏和显示 infowindow ,它有点像已刷新,图像显示在更新的 infoWindow 上。您可以通过以下方式执行此操作,

你需要保留标记引用,你可以将其作为Activity/Fragment的成员变量。

 Picasso.with(context)
                        .load(marker.getSnippet())
                        .placeholder(R.drawable.ic_placeholder)
                        .into(imageView, new Callback() {
                        @Override
                        public void onSuccess() {

                            if (currentClickedMarker != null && currentClickedMarker.isInfoWindowShown()) {
//toggle the marker's infoWindow
                                currentClickedMarker.hideInfoWindow();

                                currentClickedMarker.showInfoWindow();
                            }
                        }

                        @Override
                        public void onError() {

                        }
                    });

我也为此苦苦挣扎,这是一个从已接受的答案中获得灵感的滑行解决方案。 如果不将图片调整到合适的大小,此解决方案对我不起作用。使用 override()(和 centerCrop)它成功了。

跟踪显示的最新图片

private String previousImageUrl = null;

并用它来查看是否需要刷新当前图像

googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(final Marker marker) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_map_info_window, null);
        MyObject myObject = (MyObject) marker.getTag();
        final String url = myObject.getImageUrl();
        final ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
        GlideApp.with(getContext()).load(url)
                .override(imageWidth, imageHeight) // made the difference
                .centerCrop()
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        imageView.setImageDrawable(resource);
                        if (!TextUtils.equals(url, previousImageUrl)) {
                            previousImageUrl = url;
                            marker.showInfoWindow();
                        }
                    }
                });
        return view;
    }
});

如果您使用已接受的答案来解决问题但仍然无效, 您可能正在使用 .fit() 。 换句话说,您应该从 Picasso 代码中删除 .fit() 。 我花了几个小时才意识到这一点。