天天看點

百度地圖顯示經緯度和中文位址

百度地圖開放平台:

http://developer.baidu.com/map/geosdk-android-developv4.0.htm

1. 導入庫檔案

工程屬性->Java Build Path->Libraries中選擇“Add JARs”,

BaiduMap主包:

public class BaiduMap extends Activity {      

     public TextView tv1, tv2;

    public BMapManager mapManager;

    @Override  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.mylocation);  

        tv1=(TextView)findViewById(R.id.result1);

        tv2 = (TextView) this.findViewById(R.id.result2);

        mapManager=new BMapManager(this);

        mapManager.init("53351EE4BDE7BD870F41A0B4AF1480F1CA97DAF9", new MyMKGeneralListener());

        mapManager.getLocationManager().setNoitifyInternal(20, 5);

        mapManager.getLocationManager().requestLocationUpdates(new MyLocationListener());

        mapManager.start();

//      mapManager = new BMapManager(this);

//        mapManager.init("EDB67AD764D300895C95ABA02A4DDC58D5485CCD",

//                new MyMKGeneralListener());

//        // 設定通知間隔:iMaxSecond - 最大通知間隔,機關:秒;iMinSecond - 最小通知間隔,機關:秒

//        mapManager.getLocationManager().setNotifyInternal(20, 5);

//

//        mapManager.getLocationManager().requestLocationUpdates(

//                new MyLocationListener());

//        mapManager.start();

    }  

    // 定位自己的位置,隻定位一次  

    class MyLocationListener implements LocationListener {

        @Override

        public void onLocationChanged(Location arg0) {

            double jindu1 = arg0.getLatitude();

            double weidu1 = arg0.getLongitude();

            int jindu = (int) (arg0.getLatitude()*1000000);

            int weidu = (int) (arg0.getLongitude()*1000000);

            tv1.setText("經度:" + jindu1 + ",緯度:" + weidu1);

            System.out.println("經度:" + jindu1 + ",緯度:" + weidu1);

            MKSearch search = new MKSearch();

            search.init(mapManager, new MyMKSearchListener());

            search.reverseGeocode(new GeoPoint(jindu, weidu));

        }

    }

    class MyMKSearchListener implements MKSearchListener {

        @Override

        public void onGetAddrResult(MKAddrInfo arg0, int arg1) {

            if (arg0 == null) {

                tv2.setText("沒有擷取想要的位置");

            } else {

                GeoPoint point = arg0.geoPt;

                tv2.setText("位址:" + arg0.strAddr + ",坐标:"

                        + point.getLatitudeE6() + "," + point.getLongitudeE6());

            }

        }

        @Override

        public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {

            // TODO Auto-generated method stub

        }

        @Override

        public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {

            // TODO Auto-generated method stub

        }

        @Override

        public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {

            // TODO Auto-generated method stub

        }

        @Override

        public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {

            // TODO Auto-generated method stub

        }

    }

    // 常用事件監聽,用來處理通常的網絡錯誤,授權驗證錯誤等

    class MyMKGeneralListener implements MKGeneralListener {

        @Override

        public void onGetNetworkState(int arg0) {

        }

        @Override

        public void onGetPermissionState(int arg0) {

        }

    }

    }

BMapApiDemoApp包:

public class BMapApiDemoApp extends Application {

    static BMapApiDemoApp mDemoApp;

    //百度MapAPI的管理類

    BMapManager mBMapMan = null;    

    // 授權Key

    // TODO: 請輸入您的Key,

    // 申請位址:http://dev.baidu.com/wiki/static/imap/key/

    String mStrKey = "53351EE4BDE7BD870F41A0B4AF1480F1CA97DAF9";

    boolean m_bKeyRight = true;    // 授權Key正确,驗證通過

    // 常用事件監聽,用來處理通常的網絡錯誤,授權驗證錯誤等

    static class MyGeneralListener implements MKGeneralListener {

        @Override

        public void onGetNetworkState(int iError) {

            Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), "您的網絡出錯啦!",

                    Toast.LENGTH_LONG).show();

        }

        @Override

        public void onGetPermissionState(int iError) {

            if (iError ==  MKEvent.ERROR_PERMISSION_DENIED) {

                // 授權Key錯誤:

                Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), 

                        "請在BMapApiDemoApp.java檔案輸入正确的授權Key!",

                        Toast.LENGTH_LONG).show();

                BMapApiDemoApp.mDemoApp.m_bKeyRight = false;

            }

        }        

    }    

    @Override

    public void onCreate() {

        mDemoApp = this;

        mBMapMan = new BMapManager(this);

        mBMapMan.init(this.mStrKey, new MyGeneralListener());

        super.onCreate();

    }

    @Override

    //建議在您app的退出之前調用mapadpi的destroy()函數,避免重複初始化帶來的時間消耗

    public void onTerminate() {

        // TODO Auto-generated method stub

        if (mBMapMan != null) {

            mBMapMan.destroy();

            mBMapMan = null;

        }

        super.onTerminate();

    }

}