在Fragment中使用BaiduMap SDK

1:fragment_bshdzh.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    

    <Spinner 

        android:id="@+id/spFjName"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>

    

    <com.baidu.mapapi.map.MapView 

        android:id="@+id/bmapView"

        android:layout_width="match_parent" 

        android:layout_height="match_parent" 

        android:clickable="true"/>



</LinearLayout>

2:BshdzhFragment.java

private Spinner spFjName = null;

private MapView mMapView = null;// 地图View

private ArrayAdapter<BshAddress> adapter = null;

private BMapManager mBMapMan = null;



private MapController mMapController = null;





@Override

    public View onCreateView(LayoutInflater inflater, ViewGroup arg1, Bundle arg2) {

        //以下两行代码一定要放到最前面,否则程序会出错

        mBMapMan=new BMapManager(getActivity().getApplication()); mBMapMan.init("BQA6edrmyT8IU8tGLDIxRaFc", null); 

        

        view = inflater.inflate(R.layout.fragment_bshdzh, arg1, false);

        

        init();

        

        adapter = new ArrayAdapter<BshAddress>(

                getActivity(),

                android.R.layout.simple_spinner_item,

                UIUtil.getBshAddrArr()

            );

        

        spFjName.setAdapter(adapter);

        

        //1:读取用户设置所属分局,如果为空则默认 青岛市国家税务局

        SharedPreferences sp = getActivity().getSharedPreferences(WR.NSR_INFO, Activity.MODE_PRIVATE);

        int ssfj = sp.getInt(WR.W_SSFJ, 0); //获取所属分局,默认为青岛市国家税务局    

        

        //2:根据所属分局动态设置选中项

        spFjName.setSelection(ssfj);

        

        spFjName.setOnItemSelectedListener(fjNameListener);

        

        initMapView();    //初始化Map

        

        return view;

    }



private OnItemSelectedListener fjNameListener = new OnItemSelectedListener(){

        @Override

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

                long arg3) {

            BshAddress bshAddress = (BshAddress)spFjName.getSelectedItem();

            String lon = bshAddress.lon;

            String lat = bshAddress.lat;

            

            GeoPoint point =new GeoPoint((int)(Double.parseDouble(lat)* 1E6),(int)(Double.parseDouble(lon)* 1E6));

            //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)

            mMapController.setCenter(point);//设置地图中心点

            mMapController.setZoom(15);// 设置当前层级数为15

        }

        

        @Override

        public void onNothingSelected(AdapterView<?> arg0) {

            // TODO Auto-generated method stub

        }

    };

    

    /*

     * 初始化地图

     */

    private void initMapView(){

        mMapView = (MapView)view.findViewById(R.id.bmapView);

        mMapView.setBuiltInZoomControls(true);

        //设置启用内置的缩放控件

        mMapController=mMapView.getController();

    }

private void init(){

        spFjName = (Spinner)view.findViewById(R.id.spFjName);

    }



    @Override

    public void onDestroy() {

        // TODO Auto-generated method stub

        super.onDestroy();

        mMapView.destroy();  

        if(mBMapMan!=null){  

                mBMapMan.destroy();  

                mBMapMan=null;  

        }  

    }



    @Override

    public void onPause() {

        // TODO Auto-generated method stub

        super.onPause();

        mMapView.onPause();  

        if(mBMapMan!=null){  

            mBMapMan.stop();  

        }  

    }



    @Override

    public void onResume() {

        // TODO Auto-generated method stub

        super.onResume();

        mMapView.onResume();  

        if(mBMapMan!=null){  

            mBMapMan.start();  

        }

    }

    

3:运行结果:

在Fragment中使用BaiduMap SDK

你可能感兴趣的:(Fragment)