Android使用百度地图SDK获得当前设备位置所在的省、市

Android系统上,使用百度地图SDK获得当前设备位置所在的省、市。

具体代码实现:

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity {

	private LocationClient mLocationClient = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		MyLocationListenner myListener = new MyLocationListenner();
		mLocationClient = new LocationClient(this);
		LocationClientOption option = new LocationClientOption();
		option.setIsNeedAddress(true);
		option.setAddrType("all");
		mLocationClient.setLocOption(option);
		mLocationClient.registerLocationListener(myListener);
		mLocationClient.start();
	}

	@Override
	public void onDestroy() {
		mLocationClient.stop();
		super.onDestroy();
	}

	private class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			try {
				//当前设备位置所在的省
				String province = location.getProvince();
				
				//当前设备位置所在的市
				String city = location.getCity();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}



你可能感兴趣的:(android)