我正在使用Google Maps Api,并希望在点击标记时在InfoWindow中显示图像。我实现了它,但毕加索有一个问题。单击标记时,将显示infoWindow,imageView将显示占位符图像。如果我再次单击相同的标记,Picasso会成功加载图像。我必须点击两次,这太奇怪了。
为了更好地理解屏幕截图:
首先点击标记:
当我再次点击时:
我的代码如下:
InfoWindowAdapter(Picasso在这里加载)
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;
}
});
设置标记(使用代码段传递图片网址)
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));
}
InfoWindow layout xml
<?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 :(得分:11)
我解决了。实际上,post就是答案。但是我想解释一下如何做到这一点并提供更多细节来解决我的问题。
显示InfoWindow时,会捕获布局。这意味着InfoWindows不是动态的,它们只是一张图片,一张快照。这就是为什么我们必须在成功加载图像后刷新InfoWindow。我们可以使用Picasso的Callback来完成它。
我改变了我的代码如下:
我的新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() {}
}
设置标记(使用代码段传递图片网址):
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));
答案 1 :(得分: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();
}
}
}