百度地图动态添加marker的图片显示问题

最重要的就是加载完成图片之后在子线程添加Marker操作
网上说的通过view转成bitmap感觉没什么用


代码如下代码如下

Glide.with(mContext).load(mjbBean.getPicUrl()).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                    iconView.setBackgroundDrawable(resource);
                    //3.通过view构建BitmapDescriptor
                    BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(custom_layout);
                    //3.1构建MarkerOption,用于在地图上添加Marker
                    MarkerOptions option = new MarkerOptions().position(llA)
                            .animateType(MarkerOptions.MarkerAnimateType.grow)
                            .zIndex(finalI).period(10).title(mjbBean.getName()).icon(bitmap);
                    // 掉下动画
//                  option.animateType(MarkerOptions.MarkerAnimateType.drop);
                    overlayOptions.add(option);
                    //4.构建Marker
                    Marker marker = (Marker) mBaiduMap.addOverlay(option);
                    //4.1使用marker携带info信息,当点击事件的时候可以通过marker获得info信息
                    Bundle bundle = new Bundle();
                    // info必须实现序列化接口
                    bundle.putSerializable("info", mjbBean);
                    marker.setExtraInfo(bundle);
                }
            });


废话不多说直接上代码:

修改之前的代码(Marker图片不显示):

//百度地图--=添加Marker>自定义view
    public void addOverlaysToMap() {
        //每个marker进入的方式使用集合存放
        List<OverlayOptions> overlayOptions = new ArrayList<OverlayOptions>();
        for (int i = 0; i < jbList.size(); i++) {
            //1.自定义view
            View custom_layout= View.inflate(this, R.layout.baidu_marker_layout, null);
            final ImageView iconView = (ImageView) custom_layout.findViewById(R.id.main_maker_iv);
            TextView nameView = (TextView) custom_layout.findViewById(R.id.tv_name);
            //2.关联数据
            JdlbBean mjbBean = jbList.get(i);
            LatLng llA = new LatLng(mjbBean.getLat(), mjbBean.getLon());
            nameView.setText(mjbBean.getName());
            KLog.d(mjbBean.getPicUrl());
            Glide.with(mContext).load(mjbBean.getPicUrl()).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                    iconView.setBackgroundDrawable(resource);
                }
            });
            //设置成功后把View转换成Bitmap
            Bitmap viewBitmap = getViewBitmap(custom_layout);
            //3.通过view构建BitmapDescriptor
//            BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view);
            BitmapDescriptor bitmap = BitmapDescriptorFactory.fromBitmap(viewBitmap);
            //3.1构建MarkerOption,用于在地图上添加Marker
            MarkerOptions option = new MarkerOptions().position(llA)
                    .animateType(MarkerOptions.MarkerAnimateType.grow)
                    .zIndex(i).period(10).title(mjbBean.getName()).icon(bitmap);
            // 掉下动画
//            option.animateType(MarkerOptions.MarkerAnimateType.drop);
            overlayOptions.add(option);
            //4.构建Marker
            Marker marker = (Marker) mBaiduMap.addOverlay(option);
            //4.1使用marker携带info信息,当点击事件的时候可以通过marker获得info信息
            Bundle bundle = new Bundle();
            // info必须实现序列化接口
            bundle.putSerializable("info", mjbBean);
            marker.setExtraInfo(bundle);
        }
        // 将地图显示最开始一个marker的位置
        JdlbBean mjbBean = jbList.get(0);
        LatLng latLng = new LatLng(mjbBean.getLat(), mjbBean.getLon());
        MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
//      mBaiduMap.setMapStatus(msu);
        mBaiduMap.animateMapStatus(msu);// 动画的方式到中间
    }

修改之后的代码(多个Marker的图片都可以显示):

//百度地图--=添加Marker>自定义view
    public void addOverlaysToMap() {
        //每个marker进入的方式使用集合存放
        final List<OverlayOptions> overlayOptions = new ArrayList<OverlayOptions>();
        for (int i = 0; i < jbList.size(); i++) {
            //1.自定义view
            final View custom_layout = View.inflate(this, R.layout.baidu_marker_layout, null);
            final ImageView iconView = (ImageView) custom_layout.findViewById(R.id.main_maker_iv);
            TextView nameView = (TextView) custom_layout.findViewById(R.id.tv_name);
            //2.关联数据
            final JdlbBean mjbBean = jbList.get(i);
            final LatLng llA = new LatLng(mjbBean.getLat(), mjbBean.getLon());
            nameView.setText(mjbBean.getName());
            KLog.d(mjbBean.getPicUrl());
            final int finalI = i;
            Glide.with(mContext).load(mjbBean.getPicUrl()).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                    iconView.setBackgroundDrawable(resource);
                    //3.通过view构建BitmapDescriptor
                    BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(custom_layout);
                    //3.1构建MarkerOption,用于在地图上添加Marker
                    MarkerOptions option = new MarkerOptions().position(llA)
                            .animateType(MarkerOptions.MarkerAnimateType.grow)
                            .zIndex(finalI).period(10).title(mjbBean.getName()).icon(bitmap);
                    // 掉下动画
//                  option.animateType(MarkerOptions.MarkerAnimateType.drop);
                    overlayOptions.add(option);
                    //4.构建Marker
                    Marker marker = (Marker) mBaiduMap.addOverlay(option);
                    //4.1使用marker携带info信息,当点击事件的时候可以通过marker获得info信息
                    Bundle bundle = new Bundle();
                    // info必须实现序列化接口
                    bundle.putSerializable("info", mjbBean);
                    marker.setExtraInfo(bundle);
                }
            });
        }
        // 将地图显示最开始一个marker的位置
        JdlbBean mjbBean = jbList.get(0);
        LatLng latLng = new LatLng(mjbBean.getLat(), mjbBean.getLon());
        MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
//      mBaiduMap.setMapStatus(msu);
        mBaiduMap.animateMapStatus(msu);// 动画的方式到中间
    }


参考:http://blog.csdn.net/bob_xing_yang/article/details/53161033

猜你喜欢

转载自blog.csdn.net/a_yue10/article/details/78673930
今日推荐