一个简单易懂的小例子。用到的知识点。
1.基于百度定位SDK获取当前城市
2.使用聚合数据提供的天气信息API
3.异步发送位置信息请求天气数据
百度定位SDK的使用不再详细介绍,参考百度地图提供的资料很容易配置。给出百度地图定位SDK用法连接。http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/key
在代码中详细介绍
public class MainActivity extends AppCompatActivity {
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
TextView textView;
String mCity = "";//存放当前城市
String mCurrentLocation = "";//当前位置信息
String mTem = "";//当前温度
TextView mCitytv;//显示城市
TextView mWeathertv;//显示天气
TextView mTemtv;//显示温度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定控件
findview();
//获取位置
getLocation();
}
private void findview() {
mCitytv = (TextView) findViewById(R.id.x_CurrentCity);
mWeathertv = (TextView) findViewById(R.id.x_CurrentWeather);
mTemtv = (TextView) findViewById(R.id.x_CurrentTem);
}
/*
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
String location = bundle.getString("位置");
//将位置传入异步线程中请求天气信息
new ASyncUploadImage().execute(location);
}
};
*/
private void getLocation() {
//获取地理位置管理器
mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类
initLocation();
mLocationClient.registerLocationListener(myListener);