android获取当前城市i,Android获取当前的城市名的方法

/** Called when the activity is first created.*/

/** 上下文*/

privateBMapApiApplication mApplication;/** 定义搜索服务类*/

privateMKSearch mMKSearch;/** 记录当前经纬度的MAP*/

private HashMap mCurLocation = new HashMap();//城市名

privateString cityName;

@Overridepublic voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mApplication= (BMapApiApplication) this.getApplication();if (mApplication.mBMapMan == null) {

mApplication.mBMapMan= newBMapManager(getApplication());

mApplication.mBMapMan.init(mApplication.mStrKey,newBMapApiApplication.MyGeneralListener());

}/** 初始化MKSearch*/mMKSearch= newMKSearch();

mMKSearch.init(mApplication.mBMapMan,newMySearchListener());

}

@Overrideprotected voidonStart() {//TODO Auto-generated method stub

super.onStart();this.registerLocationListener();

}private voidregisterLocationListener() {

mApplication.mBMapMan.getLocationManager().requestLocationUpdates(

mLocationListener);if (mApplication.mBMapMan != null) {/** 开启百度地图API*/mApplication.mBMapMan.start();

}

}

@Overrideprotected voidonStop() {//TODO Auto-generated method stub

super.onStop();this.unRegisterLocationListener();

}private voidunRegisterLocationListener() {

mApplication.mBMapMan.getLocationManager().removeUpdates(

mLocationListener);if (mApplication.mBMapMan != null) {/** 终止百度地图API*/mApplication.mBMapMan.stop();

}

}

@Overrideprotected voidonDestroy() {if (mApplication.mBMapMan != null) {/** 程序退出前需调用此方法*/mApplication.mBMapMan.destroy();

mApplication.mBMapMan= null;

}

super.onDestroy();

}/** 注册定位事件*/

private LocationListener mLocationListener = newLocationListener() {

@Overridepublic voidonLocationChanged(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= newGeoPoint(latitude, longitude);/** 查询该经纬度值所对应的地址位置信息*/Weather_WelcomeActivity.this.mMKSearch

.reverseGeocode(newGeoPoint(latitude, longitude));

}catch(Exception e) {

e.printStackTrace();

}

}

}

};/** 内部类实现MKSearchListener接口,用于实现异步搜索服务*/

private classMySearchListener implements MKSearchListener {

@Overridepublic void onGetAddrResult(MKAddrInfo result, intiError) {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= newBundle();

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();

}

}

@Overridepublic voidonGetDrivingRouteResult(MKDrivingRouteResult result,intiError) {

}

@Overridepublic void onGetPoiResult(MKPoiResult result, int type, intiError) {

}

@Overridepublic voidonGetTransitRouteResult(MKTransitRouteResult result,intiError) {

}

@Overridepublic voidonGetWalkingRouteResult(MKWalkingRouteResult result,intiError) {

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值