Android百度地图开发(三)多标注及对应信息弹窗显示

接上篇Android百度地图开发(二)定位之手动定位

(本篇只给出相关代码供参考,其余代码需自行补全,望耐心参考)

1.添加标注及信息弹窗声明如下(为方便理解,声明三个标注并用不同的图标显示):

//标注
    private Marker mMarkerA;
    private Marker mMarkerB;
    private Marker mMarkerC;
    private InfoWindow mInfoWindow;
    // 初始化全局 bitmap 信息,不用时及时 recycle
    BitmapDescriptor bdA;
    BitmapDescriptor bdB;
    BitmapDescriptor bdC;

2.对声明进行赋值,写在布局初始化各种findViewById之后即可

bdA = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_marka);
        bdB = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_markb);
        bdC = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_markc);

3.添加初始化方法如下

public void initOverlay() {
        //声明三个标注点
        LatLng llA = new LatLng(39.963175, 116.400244);
        LatLng llB = new LatLng(39.942821, 116.369199);
        LatLng llC = new LatLng(39.939723, 116.425541);


        //弹窗
        MarkerOptions ooA = new MarkerOptions().position(llA).icon(bdA)
                .zIndex(7).animateType(MarkerOptions.MarkerAnimateType.grow);
        mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));

        Fire fire1 = new Fire("2017-7-7", "1", "11");
        Bundle bundle1 = new Bundle();
        bundle1.putSerializable("Fire", fire1);
        mMarkerA.setExtraInfo(bundle1);

        //弹窗
        MarkerOptions ooB = new MarkerOptions().position(llB).icon(bdB)
                .zIndex(7).animateType(MarkerOptions.MarkerAnimateType.grow);
        mMarkerB = (Marker) (mBaiduMap.addOverlay(ooB));

        Fire fire2 = new Fire("2017-7-7", "2", "22");
        Bundle bundle2 = new Bundle();
        bundle2.putSerializable("Fire", fire2);
        mMarkerB.setExtraInfo(bundle2);

        //弹窗
        MarkerOptions ooC = new MarkerOptions().position(llC).icon(bdC)
                .zIndex(7).animateType(MarkerOptions.MarkerAnimateType.grow);
        mMarkerC = (Marker) (mBaiduMap.addOverlay(ooC));

        Fire fire3 = new Fire("2017-7-7", "3", "33");
        Bundle bundle3 = new Bundle();
        bundle3.putSerializable("Fire", fire3);
        mMarkerC.setExtraInfo(bundle3);


        ArrayList giflist = new ArrayList();
        giflist.add(bdA);
        giflist.add(bdB);
        giflist.add(bdC);

    }

说明:声明三个标注点显示标注信息, 提前写好Fire实体,通过new初始化fire1、fire2、fire3,并通过setExtraInfo传入

4.在第2步代码后继续写如下代码(调用第3步的初始化方法,然后绑定标注监听事件及信息弹窗监听事件)

initOverlay();


        /**
         * 标注点击监听
         */
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            public boolean onMarkerClick(final Marker marker) {

                //弹窗点击监听
                InfoWindow.OnInfoWindowClickListener listener = null;
                listener = new InfoWindow.OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick() {

                        //点击弹窗后要执行的代码,如Toast:您点击了弹窗
                        

                    }
                };

                //弹窗要展示的信息实体
                Fire fire = new Fire();
                //获取之前传递的Fire
                fire = (Fire) marker.getExtraInfo()
                        .getSerializable("Fire");

                //mBaiduMap.hideInfoWindow();

                //渲染布局模版
                MarkerFire markerFire = new MarkerFire(
                        getActivity(), fire);

                BitmapDescriptor descriptor = BitmapDescriptorFactory
                        .fromView(markerFire);
                //获取标注坐标显示对应弹窗
                LatLng ll = marker.getPosition();
                mInfoWindow = new InfoWindow(descriptor, ll, -47, listener);
                mBaiduMap.showInfoWindow(mInfoWindow);

                return true;
            }
        });
说明:提前写好要渲染的模版MarkerFire,用于信息弹窗的布局及信息展示

5.补全其余代码,调通运行


注意:声明的实体要implements Serializable,否则会报错,模版MarkerFire的class文件及对应xml布局文件调用部分代码如下

public class MarkerFire extends LinearLayout {

    private Context mContext = null;
    private View mView = null;
    private TextView fire_time;

    public MarkerFire(Context context, Fire fire) {
        super(context);
        mContext = context;
        initView();
        setfireInfo(fire);
    }

    private void initView() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.item_fragment_marker_fire, null);
        fire_time = (TextView) mView.findViewById(R.id.marker_fire_time);
        this.addView(mView);

    }

    /**
     * 设置poi详情信息
     *
     * @param fire
     */
    private void setfireInfo(Fire fire) {

        fire_time.setText(fire.getFire_time());
       
    }

    public MarkerFire(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

}


如果还有问题或更好方法,欢迎留言讨论,如果是代码补全问题,请自行补充Android基础知识。

你可能感兴趣的:(移动端笔记)