1. 程式人生 > >Android獲取當前的城市名的方法

Android獲取當前的城市名的方法

複製程式碼
/** Called when the activity is first created. */    /** 上下文 */    private BMapApiApplication mApplication;    /** 定義搜尋服務類 */    private MKSearch mMKSearch;        /** 記錄當前經緯度的MAP*/    private HashMap<String, Double> mCurLocation = new HashMap<String, Double>();    //城市名    private String cityName;        @Override    
public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                mApplication = (BMapApiApplication) this.getApplication();        if (mApplication.mBMapMan == null) {            mApplication.mBMapMan = new BMapManager(getApplication());            mApplication.mBMapMan.init(mApplication.mStrKey,                   
new BMapApiApplication.MyGeneralListener());        }                /** 初始化MKSearch */        mMKSearch = new MKSearch();        mMKSearch.init(mApplication.mBMapMan, new MySearchListener());    }        @Override    protected void onStart() {        // TODO Auto-generated method stub        super.onStart();       
this.registerLocationListener();    }    private void registerLocationListener() {        mApplication.mBMapMan.getLocationManager().requestLocationUpdates(                mLocationListener);        if (mApplication.mBMapMan != null) {            /** 開啟百度地圖API */            mApplication.mBMapMan.start();        }    }    @Override    protected void onStop() {        // TODO Auto-generated method stub        super.onStop();        this.unRegisterLocationListener();    }    private void unRegisterLocationListener() {        mApplication.mBMapMan.getLocationManager().removeUpdates(                mLocationListener);        if (mApplication.mBMapMan != null) {            /** 終止百度地圖API */            mApplication.mBMapMan.stop();        }    }    @Override    protected void onDestroy() {        if (mApplication.mBMapMan != null) {            /** 程式退出前需呼叫此方法 */            mApplication.mBMapMan.destroy();            mApplication.mBMapMan = null;        }        super.onDestroy();    }    /** 註冊定位事件 */    private LocationListener mLocationListener = new LocationListener() {        @Override        public void onLocationChanged(Location location) {            // TODO Auto-generated method stub            if (location != null) {                try {                    int longitude = (int) (1000000 * location.getLongitude());                    int latitude = (int) (1000000 * location.getLatitude());                    /** 儲存當前經緯度 */                    mCurLocation.put("longitude", location.getLongitude());                    mCurLocation.put("latitude", location.getLatitude());                    GeoPoint point = new GeoPoint(latitude, longitude);                    /** 查詢該經緯度值所對應的地址位置資訊 */                    Weather_WelcomeActivity.this.mMKSearch                            .reverseGeocode(new GeoPoint(latitude, longitude));                } catch (Exception e) {                    e.printStackTrace();                }            }        }    };    /** 內部類實現MKSearchListener介面,用於實現非同步搜尋服務 */    private class MySearchListener implements MKSearchListener {        @Override        public void onGetAddrResult(MKAddrInfo result, int iError) {            if( iError != 0 || result == null){                Toast.makeText(Weather_WelcomeActivity.this, "獲取地理資訊失敗", Toast.LENGTH_LONG).show();            }else {                Log.info("json", "result= " + result);                cityName =result.addressComponents.city;                Bundle bundle = new Bundle();                bundle.putString("cityName", cityName.substring(0, cityName.lastIndexOf("")));                Intent intent = new Intent(Weather_WelcomeActivity.this,Weather_MainActivity.class);                intent.putExtras(bundle);                startActivity(intent);                Weather_WelcomeActivity.this.finish();            }        }        @Override        public void onGetDrivingRouteResult(MKDrivingRouteResult result,                int iError) {        }        @Override        public void onGetPoiResult(MKPoiResult result, int type, int iError) {        }        @Override        public void onGetTransitRouteResult(MKTransitRouteResult result,                int iError) {        }        @Override        public void onGetWalkingRouteResult(MKWalkingRouteResult result,                int iError) {        }    }
複製程式碼