7

私はGoogle Maps Apiを使用しており、マーカーをクリックするとInfoWindow内に画像を表示したいと考えています。私はそれを実装しましたが、ピカソについては問題があります。マーカーをクリックすると、infoWindowが表示され、imageViewにプレースホルダ画像が表示されます。もう一度同じマーカーをクリックすると、ピカソはイメージを正常に読み込みます。私は2回クリックする必要があります、それはとても奇妙です。GoogleMap.InfoWindowAdapterのピカソ画像読み込みの問題

よりよく理解するためにスクリーンショット:マーカー上

まずクリック:

First Click

私はもう一度クリックしてください:

Click again

以下の私のコードがある:

InfoWindowAdapter(ここではピカソのロード)マーカー(画像のURLを渡すスニペットを使用して)

for (MediaFeedData item : mItemList) { 
     LatLng position = new LatLng(item.getLocation().getLatitude(), 
       item.getLocation().getLongitude()); 
     mMap.addMarker(new MarkerOptions() 
       .snippet(item.getImages().getLowResolution().getImageUrl()) 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_point)) 
       .position(position)); 
    } 

情報ウィンドウのレイアウトXML設定

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

     @Override 
     public View getInfoContents(Marker marker) { 
      View myContentsView = getLayoutInflater().inflate(R.layout.map_info_content, null); 
      ImageView imageView = (ImageView) myContentsView.findViewById(R.id.imgView_map_info_content); 
      Picasso.with(MainActivity.this) 
        .load(marker.getSnippet()) 
        .placeholder(R.drawable.ic_placeholder) 
        .into(imageView); 
      return myContentsView; 
     } 
    }); 

は、

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<ImageView 
    android:id="@+id/imgView_map_info_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</LinearLayout> 
+0

同様の問題(ツイッター用ファブリック - ピカソ)、無溶液http://stackoverflow.com/questions/32715198/fabric-twitter- do-not-download-pictures-of-tweet –

+0

これはピカソに関するものではありません。 InfoWindowAdapterが正常に動作しない – sembozdemir

答えて

8

私はそれを解決しました。実際には、このpostが答えです。しかし私はそれを行う方法を説明し、私の状況でこの問題を解決するための詳細を提供したいと思います。

InfoWindowを表示すると、レイアウトがキャプチャされます。これは、InfoWindowが動的ではなく、単なる画像であり、スナップショットであることを意味します。そのため、イメージが正常に読み込まれた後、InfoWindowを更新する必要があります。ピカソのコールバックを使ってそれを行うことができます。

私のような以下の私のコードを変更:

私の新しいInfoWindowAdapterクラス:

public class MapInfoWindowAdapter implements GoogleMap.InfoWindowAdapter { 
    private final Hashtable<String, Boolean> markerSet; 
    private Context context; 
    private View myContentsView; 

    public MapInfoWindowAdapter(Context context, Hashtable<String, Boolean> markerSet) { 
     this.context = context; 
     this.markerSet = markerSet; 
    } 

    @Override 
    public View getInfoWindow(Marker marker) { 
     return null; 
    } 

    @Override 
    public View getInfoContents(Marker marker) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     myContentsView = inflater.inflate(R.layout.map_info_content, null); 
     ImageView imageView = (ImageView) myContentsView.findViewById(R.id.imgView_map_info_content); 
     boolean isImageLoaded = markerSet.get(marker.getId()); 
     if (isImageLoaded) { 
      Picasso.with(context) 
        .load(marker.getSnippet()) 
        .placeholder(R.drawable.ic_placeholder) 
        .into(imageView); 
     } else { 
      isImageLoaded = true; 
      markerSet.put(marker.getId(), isImageLoaded); 
      Picasso.with(context) 
        .load(marker.getSnippet()) 
        .placeholder(R.drawable.ic_placeholder) 
        .into(imageView, new InfoWindowRefresher(marker)); 
     } 

     return myContentsView; 
    } 
} 

を、私はそれに設定されたマーカーを渡され、それはそのマーカーのための画像を決定するためのマーカーIDとブール値を保持していますが読み込まれているかどうか私はこれをチェックして、どのPicassoコードを実行するかを選択します。私がこれをしなければ、それは再帰になります。

InfoWindowRefresher:

public class InfoWindowRefresher implements Callback { 
    private Marker markerToRefresh; 

    public InfoWindowRefresher(Marker markerToRefresh) { 
     this.markerToRefresh = markerToRefresh; 
    } 

    @Override 
    public void onSuccess() { 
     markerToRefresh.showInfoWindow(); 
    } 

    @Override 
    public void onError() {} 
} 

マーカー(画像のURLを渡すスニペットを使用して)設定:私のために働いている何

Hashtable<String, Boolean> markerSet = new Hashtable<>(); 
for (MediaFeedData item : mItemList) { 
    LatLng position = new LatLng(item.getLocation().getLatitude(), 
      item.getLocation().getLongitude()); 
    MarkerOptions markerOptions = new MarkerOptions() 
      .snippet(item.getImages().getLowResolution().getImageUrl()) 
      .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_point)) 
      .position(position); 
    Marker marker = mMap.addMarker(markerOptions); 
    markerSet.put(marker.getId(), false); 
} 

mMap.setInfoWindowAdapter(new MapInfoWindowAdapter(this, markerSet)); 
+0

非常に役に立ちました、ありがとうございました! –

+0

@sembozdemir implementsコールバックのインポートは何ですか? – TheQ

+0

@TheQ 'com.square.picasso.Callback' – sembozdemir

3

はこれです:

インサイドgetInfoWindow(マーカーマーカー):

Picasso.with(getActivity()) 
          .load(URL) 
          .error(R.drawable.my_loader) 
          .placeholder(R.drawable.my_loader) 
          .into(userPhoto, new MarkerCallback(marker,URL,userPhoto)); 

そしてクラスを作成する:

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

}

+0

ありがとう、私はこの最も簡単な方法だと思う。 – wanz

+0

@GuilhermeSimãoCoutoその 'Callback'実装のインポートは何ですか? – TheQ

+0

@TheQ import com.squareup.picasso.Callback; –

関連する問題