FragmentでのBaiduMap SDKの使用

10015 ワード

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