毕加索图片没有加载自定义infoWindow为什么?

Sam*_*ord 2 android google-maps infowindow picasso

我正在尝试以编程方式布局自定义infoWindow.我想使用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);
Run Code Online (Sandbox Code Playgroud)

我已经尝试过使用和不使用api键添加网址.

它确实可以在没有密钥的情况下点击几下,但从那时起,无论有没有.是因为它太慢,所以Android放弃并加载信息窗口没有它?有没有最好的方法来做到这一点?

另一个图像加载库会更好吗?谷歌的凌空?

还有

LinearLayout.LayoutParams

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

这是我的答案

在commonsWare新类中我添加了这个标志:

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

在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");
Run Code Online (Sandbox Code Playgroud)

派对!:)

Com*_*are 8

是因为它太慢,所以Android放弃并加载信息窗口没有它?

除非图像被缓存,否则毕加索将异步加载.并且Maps V2的工作方式是将View您返回的内容转换为a Bitmap,这就是呈现的内容.因此,您在Picasso和Maps V2之间存在竞争条件(图像是否Bitmap在创建之前加载?),因此对于任何给定的信息窗口是否有效都是不确定的.

你可以叫showInfoWindow()Marker之后毕加索加载了图像,因此您可以填充ImageView从毕加索的缓存.showInfoWindow(),在a上调用Marker,触发Maps V2重新生成信息窗口.

例如,你可以改变你现有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();
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

另一个图像加载库会更好吗?谷歌的凌空?

他们都会遭遇同样的问题.


Gui*_*uto 6

对我有用的是:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)