我用百度 map 显示从服务器获取的商铺,包含图片url。我使用 Glide 为 map 设置图标。
〉〉这是我在 map 上添加标记的方法。
private void setMarks(List<ShopList> shops) {
for(ShopList shopItem : shops){
double latitude = shopItem.getLat();
double longitude = shopItem.getLng();
LatLng latLng = new LatLng(latitude,longitude);
String shopName = shopItem.getName();
OverlayOptions textOption = new TextOptions()
.text(shopName)
.fontSize(50)
.position(latLng);
mBaiduMap.addOverlay(textOption);
Glide.with(mContext.getApplicationContext())
.load(shopItem.getCategory_image())
.asBitmap()
.placeholder(R.drawable.ic_shop_image_loading)
.error(R.drawable.ic_shop_image_load_error)
.override(SizeUtils.dip2px(mContext,128),SizeUtils.dip2px(mContext,128))
.centerCrop()
.into(target);
}
}
这里是 Glide 回调代码。
private SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(resource);
Marker marker = (Marker) mBaiduMap.addOverlay(new MarkerOptions().position(latLng).icon(descriptor));
mMarkers.add(marker);
}
};
‖我无法传递latLang的参数,所以我无法在onResourceReady中初始化Marker,也无法将Marker添加到mMarkers。我该怎么做才能将 latLang 与特定位图相关联?
最佳答案
您必须创建自定义 Target
。
public class MyTarget extends SimpleTarget<Bitmap> {
private final LatLng latLng;
public MyTarget(LatLng latLng) {
this.latLng = latLng;
}
@Override
public void onResourceReady(final Bitmap resource, final GlideAnimation<? super Bitmap> glideAnimation) {
// use your `latLng`
}
}
并以这种方式使用:
Glide.with(...)
...
.into(new MyTarget(latLng));
关于android - 如何将参数传递给 Glide Callback 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43203843/