如何将参数传递给滑行回调方法



我使用百度地图显示从服务器获得的商店,其中包含图片网址。我使用滑行来设置地图的图标。

这是我用于将标记添加到地图的方法。

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

  
  这是滑翔回调代码。

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 中初始化标记,也无法将标记添加到 mMarks。我该怎么做才能将 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));

最新更新