将Glide与GoogleMap.InfoWindowAdapter一起使用

时间:2015-11-09 09:23:25

标签: android google-maps-api-2 android-glide

我正在尝试使用Glide将自定义图像加载到Google地图信息窗口。我将活动上下文传递给适配器(请参阅下面的代码)。但我既没有看到图像也没有看到错误。

我做错了什么?

class GuideMapInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private final View mWindow;
    private final View mContents;
    private HashMap<Marker, MapPoint> mMarkers;
    private Context mContext;

    GuideMapInfoWindowAdapter(Context context, HashMap<Marker, MapPoint> markerMapPointHashMap) {
        mWindow = getLayoutInflater().inflate(R.layout.guide_map_custom_info_window, null);
        mContents = getLayoutInflater().inflate(R.layout.guide_map_custom_info_content, null);
        mMarkers = markerMapPointHashMap;
        mContext = context;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        render(marker, mWindow);
        return mWindow;
    }

    @Override
    public View getInfoContents(Marker marker) {
        render(marker, mContents);
        return mContents;
    }

    private void render(Marker marker, View view) {
        ImageView mImage = (ImageView) view.findViewById(R.id.badge);
        MapPoint mPoint = markers.get(marker);
        String mUrl = UrlBuilder.guidePhoto(mPoint.getGuideId(), mPoint.getPageId(), 100);
        Glide.with(mContext)
                .load(mUrl)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mImage);

        String title = marker.getTitle();
        TextView titleUi = ((TextView) view.findViewById(R.id.title));
        if (title != null) {
            titleUi.setText(title);
        } else {
            titleUi.setText("");
        }

        String snippet = marker.getSnippet();
        TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
        if (snippet != null) {
            snippetUi.setText(snippet);
        } else {
            snippetUi.setText("");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题是,您是异步下载图像。在mContents中返回getInfoContents后,对imageview的任何更改都不会反映出来。

我已经更新了你的渲染功能,但还没有测试过。

private void render(Marker marker, View view) {
        ImageView mImage = (ImageView) view.findViewById(R.id.badge);
        MapPoint mPoint = markers.get(marker);
        String mUrl = UrlBuilder.guidePhoto(mPoint.getGuideId(), mPoint.getPageId(), 100);
        Bitmap image = Glide.with(mContext)
                .load(mUrl)
                .asBitmap()
                .into(-1,-1)
                .get();
        mImage.setImageBitmap(image);

        String title = marker.getTitle();
        TextView titleUi = ((TextView) view.findViewById(R.id.title));
        if (title != null) {
            titleUi.setText(title);
        } else {
            titleUi.setText("");
        }

        String snippet = marker.getSnippet();
        TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
        if (snippet != null) {
            snippetUi.setText(snippet);
        } else {
            snippetUi.setText("");
        }
    }