在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android天气预报实现定位与天气报道

android天气预报实现定位与天气报道

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:2.95M
  • 下载次数:107
  • 浏览次数:945
  • 发布时间:2016-07-19
  • 实例类别:Android平台开发
  • 发 布 人:sjn
  • 文件格式:.zip
  • 所需积分:1
 相关标签: Android 天气预报 定位 天气 d

实例介绍

【实例简介】

此项目实现了定位功能,可以查询今天及未来四天的天气情况,采用了一个二级联动的城市选择器

【实例截图】

【核心代码】

mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
mLocationClient.registerLocationListener(myListener); // 注册监听函数
initLocation();
// 开始定位
mLocationClient.start();



}

/*@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Intent intent = getIntent();
if(!"".equals(intent.getStringExtra("cityname"))){
requestJuHeData(intent.getStringExtra("cityname"));
}
}*/

private void initLocation() {
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");// 可选,默认gcj02,设置返回的定位结果坐标系
option.setScanSpan(0);// 可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);// 可选,默认false,设置是否使用gps
option.setLocationNotify(true);// 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIsNeedLocationDescribe(true);// 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationPoiList(true);// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);// 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.SetIgnoreCacheException(false);// 可选,默认false,设置是否收集CRASH信息,默认收集
option.setEnableSimulateGps(false);// 可选,默认false,设置是否需要过滤gps仿真结果,默认需要
mLocationClient.setLocOption(option);
}

// 关联控件
public void initWdget() {
tv_city = (TextView) findViewById(R.id.tv_city);
tv_temp = (TextView) findViewById(R.id.tv_temp);
tv_wind = (TextView) findViewById(R.id.tv_wind);
tv_date = (TextView) findViewById(R.id.tv_date);
seCity = (Button) findViewById(R.id.seCity);
today = (TextView) findViewById(R.id.today);
today1 = (TextView) findViewById(R.id.today1);
today2 = (TextView) findViewById(R.id.today2);
tomorrow = (TextView) findViewById(R.id.tomorrow);
tomorrow1 = (TextView) findViewById(R.id.tomorrow1);
tomorrow2 = (TextView) findViewById(R.id.tomorrow2);
nextto = (TextView) findViewById(R.id.nextto);
nextto1 = (TextView) findViewById(R.id.nextto1);
nextto2 = (TextView) findViewById(R.id.nextto2);
nexttoo = (TextView) findViewById(R.id.nexttoo);
nexttoo1 = (TextView) findViewById(R.id.nexttoo1);
nexttoo2 = (TextView) findViewById(R.id.nexttoo2);
nexttooo = (TextView) findViewById(R.id.nexttooo);
nexttooo1 = (TextView) findViewById(R.id.nexttooo1);
nexttooo2 = (TextView) findViewById(R.id.nexttooo2);
tv_hint = (TextView)findViewById(R.id.tv_hint);
        yundong = (TextView)findViewById(R.id.yundong);
        yundongindex = (TextView)findViewById(R.id.yundongindex);
        xiche = (TextView)findViewById(R.id.xiche);
        xicheindex = (TextView)findViewById(R.id.xicheindex);
        ziwaixian = (TextView)findViewById(R.id.ziwaixian);
        ziwaixianindex = (TextView)findViewById(R.id.ziwaixianindex);
        guangjie = (TextView)findViewById(R.id.guangjie);
        guangjieindex = (TextView)findViewById(R.id.guangjieindex);
        fangshai = (TextView)findViewById(R.id.fangshai);
        fangshaiindex = (TextView)findViewById(R.id.fangshaiindex);
        diaoyu = (TextView)findViewById(R.id.diaoyu);
        diaoyuindex = (TextView)findViewById(R.id.diaoyuindex);
        chuanyi = (TextView)findViewById(R.id.chuanyi);
        chuanyiindex = (TextView)findViewById(R.id.chuanyiindex);
        shushi = (TextView)findViewById(R.id.shushi);
        shushiindex = (TextView)findViewById(R.id.shushiindex);
        
        
}

public void setImage(TextView tv, String weather) {
if ("阴转多云".equals(weather)) {
tv.setBackgroundResource(R.drawable.yinzhuanduoyun);

} else if ("晴".equals(weather)) {
tv.setBackgroundResource(R.drawable.qing);
} else if ("多云".equals(weather)) {
tv.setBackgroundResource(R.drawable.duoyun);
} else if ("雷阵雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.leizhenyu);
} else if ("小雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.xiaoyu);
} else if ("中雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.zhongyu);
} else if ("暴雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.baoyu);
} else if ("大雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.dayu);
} else if ("阵雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.zhenyu);
} else if ("中雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.zhongyu);
}else if ("阴转阵雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.yinzhuanzhenyu);
}else if ("晴转阴".equals(weather)) {
tv.setBackgroundResource(R.drawable.qingzhuanyin);
}
}

public void requestJuHeData(String cityname) {
Parameters param = new Parameters();
param.add("cityname", cityname);
param.add("dtype", "json");
JuheData.executeWithAPI(MainActivity.this, // 上下文对象
39, // 数据ID
"http://v.juhe.cn/weather/index",// 接口地址
JuheData.GET,// 请求方法
param,// 传入参数
new DataCallBack() {
// 请求失败后执行
@Override
public void onFailure(int arg0, String arg1, Throwable arg2) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, arg2.toString(),
Toast.LENGTH_SHORT).show();
}

// 不管成功与否,都执行该方法
@Override
public void onFinish() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "finish",
Toast.LENGTH_SHORT).show();
}

// 请求成功后执行
@Override
public void onSuccess(int arg0, String arg1) {
// TODO Auto-generated method stub
try {
JSONObject json = new JSONObject(arg1);
String resultcode = json.getString("resultcode");
if ("200".equals(resultcode)) {
JSONObject result = json
.getJSONObject("result");
JSONObject jstoday = result
.getJSONObject("today");
JSONObject sk = result.getJSONObject("sk");
JSONObject future = result
.getJSONObject("future");
String city = jstoday.getString("city");
String temp = sk.getString("temp");
String wind = jstoday.getString("wind");
String date = jstoday.getString("date_y");
String reg = "[\u4e00-\u9fa5]";
Pattern pat = Pattern.compile(reg);
Matcher mat = pat.matcher(date);
todayDate = mat.replaceAll("");
tv_city.setText(city);
tv_temp.setText(temp "℃");
tv_wind.setText(wind);
tv_date.setText(date);
today.setText("今天");
String todaywe = jstoday.getString("weather");
setImage(today1,todaywe);
today2.setText(jstoday.getString("temperature"));
tomorrow.setText("明天");
String tomorrowDate = (Integer.parseInt(todayDate) 1) "";
JSONObject tomorrow = future
.getJSONObject("day_" tomorrowDate);
String tomorrowwe = tomorrow.getString("weather");
setImage(tomorrow1,tomorrowwe);
tomorrow2.setText(tomorrow.getString("temperature"));
String nextDate = (Integer.parseInt(todayDate) 2) "";
JSONObject nextday = future
.getJSONObject("day_" nextDate);
String nextdaywe = nextday.getString("weather");
nextto.setText("后天");
setImage(nextto1,nextdaywe);
nextto2.setText(nextday.getString("temperature"));
String nexttooDate = (Integer.parseInt(todayDate) 3) "";
JSONObject nexttooday = future
.getJSONObject("day_" nexttooDate);
nexttoo.setText(nexttooday.getString("week"));
String nexttoowe = nexttooday.getString("weather");
setImage(nexttoo1,nexttoowe);
nexttoo2.setText(nexttooday.getString("temperature"));
String nexttoooDate = (Integer.parseInt(todayDate) 4) "";
JSONObject nexttoooday = future
.getJSONObject("day_" nexttoooDate);
nexttooo.setText(nexttoooday.getString("week"));
String nexttooowe = nexttoooday.getString("weather");
setImage(nexttooo1,nexttooowe);
nexttooo2.setText(nexttoooday.getString("temperature"));
tv_hint.setText(jstoday.getString("dressing_advice"));
yundong.setBackgroundResource(R.drawable.yundong);
yundongindex.setText(jstoday.getString("exercise_index"));
xiche.setBackgroundResource(R.drawable.xiche);
xicheindex.setText(jstoday.getString("wash_index"));
ziwaixian.setBackgroundResource(R.drawable.ziwaixian);
ziwaixianindex.setText(jstoday.getString("uv_index"));
guangjie.setBackgroundResource(R.drawable.guangjie);
guangjieindex.setText(jstoday.getString("travel_index"));
fangshai.setBackgroundResource(R.drawable.fangshai);
fangshaiindex.setText("较" jstoday.getString("uv_index"));
diaoyu.setBackgroundResource(R.drawable.diaoyu);
diaoyuindex.setText(jstoday.getString("exercise_index"));
chuanyi.setBackgroundResource(R.drawable.chuanyi);
chuanyiindex.setText(jstoday.getString("dressing_index"));
shushi.setBackgroundResource(R.drawable.shushi);
shushiindex.setText("较舒适");
}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/*
* Toast.makeText( MainActivity.this,
* arg1,Toast.LENGTH_SHORT). show();
*/
Log.i("this", arg1);
}

});
;
}

public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// Receive Location
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
sb.append("\nspeed : ");
sb.append(location.getSpeed());// 单位:公里每小时
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\nheight : ");
sb.append(location.getAltitude());// 单位:米
sb.append("\ndirection : ");
sb.append(location.getDirection());// 单位度
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\ndescribe : ");
sb.append("gps定位成功");

} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
sb.append("\naddr : ");
sb.append(location.getAddrStr());
// 运营商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());
sb.append("\ndescribe : ");
sb.append("网络定位成功");
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
sb.append("\ndescribe : ");
sb.append("离线定位成功,离线定位结果也是有效的");
} else if (location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("网络不同导致定位失败,请检查网络是否通畅");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
sb.append("\nlocationdescribe : ");
sb.append(location.getLocationDescribe());// 位置语义化信息
List<Poi> list = location.getPoiList();// POI数据
if (list != null) {
sb.append("\npoilist size = : ");
sb.append(list.size());
for (Poi p : list) {
sb.append("\npoi= : ");
sb.append(p.getId() " " p.getName() " " p.getRank());
}
}
Log.i("tianqi", sb.toString());
city = location.getCity().toString()
.substring(0, location.getCity().indexOf("市"));
Toast.makeText(MainActivity.this, location.getCity(),
Toast.LENGTH_SHORT).show();


Intent intent = getIntent();
if(!"".equals(intent.getStringExtra("cityname")) && intent.getStringExtra("cityname")!=null){
requestJuHeData(intent.getStringExtra("cityname"));
/*Toast.makeText(MainActivity.this, intent.getStringExtra("cityname") "456", Toast.LENGTH_SHORT).show();*/
}else{
requestJuHeData(city);
/*Toast.makeText(MainActivity.this, city "123", Toast.LENGTH_SHORT).show();*/
}
}
}

实例下载地址

android天气预报实现定位与天气报道

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 虾米 发表于: 2016-08-08 09:25 57
为什么报错了?进不了,直接退出

支持(0) 盖楼(回复)

第 2 楼 123yq 发表于: 2018-05-18 21:44 25
我的选好城市后,不能显示出现的天气状况,我把openid 和id配置了,只是key没找到在哪

支持(0) 盖楼(回复)

第 3 楼 123yq 发表于: 2018-05-18 21:44 30
我的选好城市后,不能显示出现的天气状况,我把openid 和id配置了,只是key没找到在哪

支持(0) 盖楼(回复)

第 4 楼 123yq 发表于: 2018-05-18 21:44 30
我的选好城市后,不能显示出现的天气状况,我把openid 和id配置了,只是key没找到在哪

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有5条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警