一个简单易懂的小例子。用到的知识点。
百度定位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); //注册监听函数
mLocationClient.start();
mLocationClient.requestLocation();
}
private void initLocation() {
LocationClientOption option = new LocationClientOption();
option.setIsNeedAddress(true);
option.setOpenGps(true);
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("gcj02");//可选,默认gcj02,设置返回的定位结果坐标系
//int span = 1000;
//option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
mLocationClient.setLocOption(option);
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//Receive Location
//获取城市
String city = location.getCity();
/*Bundle bundle = new Bundle();
bundle.putString("位置", city);
Message msg=Message.obtain();
msg.setData(bundle);
msg.what = 0;
handler.sendMessage(msg);*/
// handler.sendEmptyMessage(0);
mCitytv.setText(city);
ASyncUploadImage as = new ASyncUploadImage();
as.execute(city);
}
}
private class ASyncUploadImage extends AsyncTask<String, Void, String> {
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//处理接收到的json字符串 这个方法是在UI线程执行,所以能直接更新界面
getJson(s);
}
@Override
protected String doInBackground(String... params) {
//返回接收到的数据
return getData(params[0]);
}
}
private String getData(String string) {
//将传进来的城市名转为utf-8格式
String strUTF8 = null;
try {
strUTF8 = URLDecoder.decode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//拼接url地址
String url = "http://op.juhe.cn/onebox/weather/query?cityname=" + strUTF8 + "&key=a7c1f7ebf5f9484e8e5722f54e66d227";
Log.i("TAG", "url=" + url);
InputStreamReader isr;
String result = "";
try {
//获取url的输入流
isr = new InputStreamReader(new URL(url).openStream(), "utf-8");
BufferedReader br = new BufferedReader(isr);
String line = "";
//将输入流中的数据保存到字符串result中
while ((line = br.readLine()) != null) {
result += line;
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private void getJson(String json) {
//json字符串处理
try {
JSONObject jsonObject = new JSONObject(json);
String reason = jsonObject.getString("reason");
Log.i("TAG", "reason=" + reason);
String result = jsonObject.getString("result");
Log.i("TAG", "result=" + result);
JSONObject jsonResult = new JSONObject(result);
String data = jsonResult.getString("data");
Log.i("TAG", "data=" + data);
JSONObject jsonData = new JSONObject(data);
String realtime = jsonData.getString("realtime");
Log.i("TAG", "realtime=" + realtime);
JSONObject jsonRealtime = new JSONObject(realtime);
String weather = jsonRealtime.getString("weather");
JSONObject jsonWeather = new JSONObject(weather);
String info = jsonWeather.getString("info");
Log.i("TAG", "info=" + info);
String temperature = jsonWeather.getString("temperature");
Log.i("TAG", "temperature=" + temperature);
//将读取到的信息显示到界面
//显示温度
mTemtv.setText(temperature);
//显示天气
mWeathertv.setText(info);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
JSON数据示例
{
“reason”: “查询成功”,
“result”: {
“data”: {
“realtime”: {
“city_code”: “101210701”,
“city_name”: “温州”, /城市/
“date”: “2014-10-15”, /日期/
“time”: “09:00:00”, /更新时间/
“week”: 3,
“moon”: “九月廿二”,
“dataUptime”: 1413337811,
“weather”: { /当前实况天气/
“temperature”: “19”, /温度/
“humidity”: “54”, /湿度/
“info”: “雾”,
“img”: “18” /*18是雾这种天气所对应的图片的ID,每种天气的图片需要您自己设计,或者请阅读
https://www.juhe.cn/docs/api/id/39/aid/117*/
},
“wind”: {
“direct”: “北风”,
“power”: “1级”,
“offset”: null,
“windspeed”: null
}
},
“life”: { /生活指数/
“date”: “2014-10-15”,
“info”: {
“chuanyi”: [ /穿衣指数/
“较舒适”,
“建议着薄外套或牛仔衫裤等服装。年老体弱者宜着夹克衫、薄毛衣等。昼夜温差较大,注意适当增减衣服。”
],
“ganmao”: [ /感冒指数/
“较易发”,
“昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。”
],
“kongtiao”: [ /空调指数/
“较少开启”,
“您将感到很舒适,一般不需要开启空调。”
],
“wuran”: [ /污染指数/
“良”,
“气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。”
],
“xiche”: [ /洗车指数/
“较适宜”,
“较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。”
],
“yundong”: [ /运动指数/
“较适宜”,
“天气较好,但风力较大,推荐您进行室内运动,若在户外运动请注意防风。”
],
“ziwaixian”: [ /紫外线/
“中等”,
“属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。”
]
}
},
“weather”: [ /未来几天天气预报/
{
“date”: “2014-10-15”,
“info”: {
“day”: [ /白天天气/
“0”, /天气ID/
“晴”, /天气/
“24”, /高温/
“东北风”, /风向/
“3-4 级” /风力/
],
“night”: [ /夜间天气/
“0”,
“晴”,
“13”,
“东北风”,
“3-4 级”
]
},
“week”: “三”,
“nongli”: “九月廿二”
},
{
“date”: “2014-10-16”,
“info”: {
“dawn”: [
“0”,
“晴”,
“13”,
“东北风”,
“3-4 级”
],
“day”: [
“0”,
“晴”,
“25”,
“东北风”,
“3-4 级”
],
“night”: [
“1”,
“多云”,
“15”,
“东北风”,
“3-4 级”
]
},
“week”: “四”,
“nongli”: “九月廿三”
},
{
“date”: “2014-10-17”,
“info”: {
“dawn”: [
“1”,
“多云”,
“15”,
“东北风”,
“3-4 级”
],
“day”: [
“1”,
“多云”,
“26”,
“东北风”,
“3-4 级”
],
“night”: [
“1”,
“多云”,
“16”,
“东北风”,
“3-4 级”
]
},
“week”: “五”,
“nongli”: “九月廿四”
},
{
“date”: “2014-10-18”,
“info”: {
“dawn”: [
“1”,
“多云”,
“16”,
“东北风”,
“3-4 级”
],
“day”: [
“1”,
“多云”,
“26”,
“东风”,
“3-4 级”
],
“night”: [
“1”,
“多云”,
“18”,
“东风”,
“3-4 级”
]
},
“week”: “六”,
“nongli”: “九月廿五”
},
{
“date”: “2014-10-19”,
“info”: {
“dawn”: [
“1”,
“多云”,
“18”,
“东风”,
“3-4 级”
],
“day”: [
“1”,
“多云”,
“27”,
“东风”,
“3-4 级”
],
“night”: [
“1”,
“多云”,
“19”,
“东南风”,
“3-4 级”
]
},
“week”: “日”,
“nongli”: “九月廿六”
},
{
“date”: “2014-10-20”,
“info”: {
“dawn”: [
“1”,
“多云”,
“19”,
“东南风”,
“3-4 级”
],
“day”: [
“1”,
“多云”,
“27”,
“东南风”,
“3-4 级”
],
“night”: [
“2”,
“阴”,
“18”,
“南风”,
“3-4 级”
]
},
“week”: “一”,
“nongli”: “九月廿七”
},
{
“date”: “2014-10-21”,
“info”: {
“dawn”: [
“2”,
“阴”,
“18”,
“南风”,
“3-4 级”
],
“day”: [
“1”,
“多云”,
“26”,
“东北风”,
“3-4 级”
],
“night”: [
“2”,
“阴”,
“17”,
“”,
“微风”
]
},
“week”: “二”,
“nongli”: “九月廿八”
}
],
“pm25”: { /PM2.5/
“key”: “Wenzhou”,
“show_desc”: 0,
“pm25”: {
“curPm”: “97”,
“pm25”: “72”,
“pm10”: “97”,
“level”: 2,
“quality”: “良”,
“des”: “可以接受的,除极少数对某种污染物特别敏感的人以外,对公众健康没有危害。”
},
“dateTime”: “2014年10月15日09时”,
“cityName”: “温州”
},
“date”: null,
“isForeign”: 0
}
},
“error_code”: 0
}
https://www.juhe.cn/docs/api/id/73