Search code examples
androidandroid-glidebaidu-map

How to deliver parameters to Glide Callback method?


  I use Baidu Map to display shops got from server, containing picture url. I use Glide to set Icons for map.

  Here is my method which using to add Marker to 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);                                        
    }
}  

  
  Here is Glide Callback code.

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

};  

  I could not deliver parameter of latLang, so I can not init Marker in onResourceReady, can not add Marker to mMarkers also. What can I do to relate latLang to specific Bitmap?


Solution

  • You have to create your custom 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`
        }
    }
    

    And use this way:

    Glide.with(...)
        ...                                                    
        .into(new MyTarget(latLng));