通过百度地图获取当地天气信息

首先,通过之前用过的百度地图,实现定位功能;首先去百度开放云申请密钥,这里就不说明申请过程了,不懂得可以百度一下。

申请完密钥后,需要去声明Activity的地方添加上下面的语句:

[html]  view plain  copy
  1. <meta-data  
  2.  android:name="com.baidu.lbsapi.API_KEY"  
  3.  android:value="dGozMoOnqaOgcpaj3f3NBtxh" />  
Value值 即填写申请的密钥;

声明定位:

[java]  view plain  copy
  1. // 定位相关  
  2. public LocationClient mLocationClient = null;  
  3. public BDLocationListener myListener = new MyLocationListener();  

[java]  view plain  copy
  1. mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类  
  2. mLocationClient.registerLocationListener(myListener); // 注册监听函数  
  3.   
  4. LocationClientOption option = new LocationClientOption();  
  5. option.setOpenGps(true);// 打开gps  
  6. option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02  
  7. // option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms,如果不设置,默认只定位一次  
  8. option.setIsNeedAddress(true);// 返回的定位结果包含地址信息  
  9. option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向  
  10. option.setLocationMode(com.baidu.location.LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式  
  11. option.setProdName(getPackageName());  
  12. mLocationClient.setLocOption(option);  
[java]  view plain  copy
  1. mLocationClient.start();  

定义监听器:

[java]  view plain  copy
  1. // 定位  
  2.     public class MyLocationListener implements BDLocationListener {  
  3.         @Override  
  4.         public void onReceiveLocation(BDLocation location) {  
  5.             if (location == null)  
  6.                 return;  
  7.             StringBuffer sb = new StringBuffer(256);  
  8.             sb.append("time : ");  
  9.             sb.append(location.getTime());  
  10.             sb.append("\nerror code : ");  
  11.             sb.append(location.getLocType());  
  12.             sb.append("\nlatitude : ");  
  13.             sb.append(location.getLatitude());  
  14.             sb.append("\nlontitude : ");  
  15.             sb.append(location.getLongitude());  
  16.             sb.append("\nradius : ");  
  17.             sb.append(location.getRadius());  
  18.             if (location.getLocType() == BDLocation.TypeGpsLocation) {  
  19.                 sb.append("\nspeed : ");  
  20.                 sb.append(location.getSpeed());  
  21.                 sb.append("\nsatellite : ");  
  22.                 sb.append(location.getSatelliteNumber());  
  23.             } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {  
  24.                 sb.append("\naddr : ");  
  25.                 sb.append(location.getAddrStr());  
  26.             }  
  27.   
  28.             String place = location.getAddrStr();  
  29. //          locationplace.setText(location.getCity());  
  30.             localcity.setCity(location.getCity());  
  31.   
  32.             Toast.makeText(getApplicationContext(), "" + place, 1).show();  
  33.         }  
  34.   
  35.     }  
下面就是如何获取天气:

获取天气的接口,网上有很多,但有的 已经不能再用了,而且调用的时候 很不稳定,数据不够准确;这里是网上找的一个网友搜集的比较全的一个天气api接口:

http://www.eoeandroid.com/thread-333874-1-1.html

这里我用的是:http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101040100&weatherType=0

感觉还不错,其中cityCode是城市的代号,type  为0的时候 是最近几天的详细天气,为1的时候 是当前温度信息;

首先定义一个Weather类:

[java]  view plain  copy
  1. package com.example.util;  
  2.   
  3. public class Weather {  
  4.     private String city;  
  5.     private String cityId;  
  6.     private String toptemp;  
  7.       
  8.     private String lowtemp;  
  9.     private String weather;  //天气  
  10.     private String wind;     //风向  
  11.     private String windfl;   //风力  
  12.       
  13.     private String gxtime;  
  14.     private String index;   //穿衣指数  
  15.     private String index_d; //穿衣建议  
  16.   
  17.     public String getCity() {  
  18.         return city;  
  19.     }  
  20.   
  21.     public void setCity(String city) {  
  22.         this.city = city;  
  23.     }  
  24.   
  25.     public String getCityId() {  
  26.         return cityId;  
  27.     }  
  28.   
  29.     public void setCityId(String cityId) {  
  30.         this.cityId = cityId;  
  31.     }  
  32.   
  33.     public String getToptemp() {  
  34.         return toptemp;  
  35.     }  
  36.   
  37.     public void setToptemp(String toptemp) {  
  38.         this.toptemp = toptemp;  
  39.     }  
  40.   
  41.     public String getLowtemp() {  
  42.         return lowtemp;  
  43.     }  
  44.   
  45.     public void setLowtemp(String lowtemp) {  
  46.         this.lowtemp = lowtemp;  
  47.     }  
  48.   
  49.     public String getWeather() {  
  50.         return weather;  
  51.     }  
  52.   
  53.     public void setWeather(String weather) {  
  54.         this.weather = weather;  
  55.     }  
  56.   
  57.     public String getGxtime() {  
  58.         return gxtime;  
  59.     }  
  60.   
  61.     public void setGxtime(String gxtime) {  
  62.         this.gxtime = gxtime;  
  63.     }  
  64.   
  65.     public String getWind() {  
  66.         return wind;  
  67.     }  
  68.   
  69.     public void setWind(String wind) {  
  70.         this.wind = wind;  
  71.     }  
  72.   
  73.     public String getWindfl() {  
  74.         return windfl;  
  75.     }  
  76.   
  77.     public void setWindfl(String windfl) {  
  78.         this.windfl = windfl;  
  79.     }  
  80.   
  81.     public String getIndex() {  
  82.         return index;  
  83.     }  
  84.   
  85.     public void setIndex(String index) {  
  86.         this.index = index;  
  87.     }  
  88.   
  89.     public String getIndex_d() {  
  90.         return index_d;  
  91.     }  
  92.   
  93.     public void setIndex_d(String index_d) {  
  94.         this.index_d = index_d;  
  95.     }  
  96.       
  97.   
  98. }  

获取天气的方法:

[java]  view plain  copy
  1. public void GetFirstWeather(){  
  2.         URL url1, url2;  
  3.         if (isInterent.hasInternet(this)) {  
  4.             mLocationClient.requestLocation();  
  5.             String city = localcity.getCity();  
  6.             if (city.contains("市") || city.contains("省")) {   
  7.                 City = city.substring(0, city.length() - 1);  
  8.             }  
  9.   
  10.             try {  
  11.                 url1 = new URL(  
  12.                         String.format(getString(R.string.weatherurl)  
  13.                                 + "?cityCode=%1$s&weatherType=0",  
  14.                                 LocalCity.getCityIdByName(City)));  
  15.   
  16.                 GetWeather nt = new GetWeather(url1, "0");  
  17.                 nt.start();  
  18.   
  19.                 url2 = new URL(  
  20.                         String.format(getString(R.string.weatherurl)  
  21.                                 + "?cityCode=%1$s&weatherType=1",  
  22.                                 LocalCity.getCityIdByName(City)));  
  23.   
  24.                 GetWeather nt1 = new GetWeather(url2, "1");  
  25.                 nt1.start();  
  26.   
  27.             } catch (MalformedURLException e) {  
  28.                 // TODO Auto-generated catch block  
  29.                 e.printStackTrace();  
  30.             }  
  31.         } else {  
  32.             Toast.makeText(getApplicationContext(), "网络异常,请检查网络是否连接",  
  33.                     Toast.LENGTH_LONG).show();  
  34.         }  
  35.     }  

[java]  view plain  copy
  1. class GetWeather extends Thread {  
  2.     private URL url;  
  3.     private String IsNow;  
  4.   
  5.     public GetWeather(URL url, String IsNow) {  
  6.         this.url = url;  
  7.         this.IsNow = IsNow;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void run() {  
  12.         HttpURLConnection conn = null// 连接对象  
  13.         String resultData = HttpUtil.getResault(conn, url);  
  14.   
  15.         Log.i("天气", resultData);  
  16.         String resultmsg = null;  
  17.   
  18.         Looper.prepare();  
  19.         if (IsNow.equals("0")) {  
  20.             Message msg = new Message();  
  21.             resultmsg = json(resultData);  
  22.             msg.what = 1;  
  23.             msg.obj = resultmsg;  
  24.             handler.sendMessage(msg);  
  25.         } else {  
  26.             Message msg1 = new Message();  
  27.             resultmsg = json1(resultData);  
  28.             msg1.what = 2;  
  29.             msg1.obj = resultmsg;  
  30.             handler.sendMessage(msg1);  
  31.         }  
  32.         Looper.loop();  
  33.   
  34.     }  
  35.   
  36. }  

[java]  view plain  copy
  1. // 解析天气的json数据  
  2. public String json(String json) {  
  3.     String jsonresult = null;  
  4.     Weather weather = new Weather();  
  5.     try {  
  6.         obj = new JSONObject(json);  
  7.         JSONObject contentObject = obj.getJSONObject("weatherinfo");  
  8.         weather.setCity(contentObject.getString("city"));  
  9.         weather.setToptemp(contentObject.getString("temp1"));  
  10.         weather.setWeather(contentObject.getString("weather1"));  
  11.         weather.setIndex(contentObject.getString("index"));  
  12.         weather.setIndex_d(contentObject.getString("index_d"));  
  13.   
  14.         jsonresult = weather.getCity() + ":  " + weather.getToptemp()  
  15.                 + "\n天气:" + weather.getWeather() + "   "  
  16.                 +"\n穿衣指数:" + weather.getIndex() + "\n" + weather.getIndex_d();  
  17.     } catch (JSONException e) {  
  18.         Log.i("Tag""解析json失败");  
  19.         e.printStackTrace();  
  20.     }  
  21.     weather = null;  
  22.     return jsonresult;  
  23. }  

[java]  view plain  copy
  1. // 解析实时天气的json数据  
  2.     public String json1(String json) {  
  3.         String jsonresult = null;  
  4.         Weather weather = new Weather();  
  5.         try {  
  6.             obj = new JSONObject(json);  
  7.             JSONObject contentObject = obj.getJSONObject("weatherinfo");  
  8.             weather.setToptemp(contentObject.getString("temp"));  
  9.             weather.setWind(contentObject.getString("WD"));  
  10.             weather.setWindfl(contentObject.getString("WS"));  
  11.             weather.setGxtime(contentObject.getString("time"));  
  12.   
  13.             jsonresult = "当前温度:" + weather.getToptemp() + "  风向风力:  "  
  14.                     + weather.getWind() + weather.getWindfl() + "  "  
  15.                     + weather.getGxtime() + "发布";  
  16.         } catch (JSONException e) {  
  17.             Log.i("Tag""解析json失败");  
  18.             e.printStackTrace();  
  19.         }  
  20.         weather = null;  
  21.         return jsonresult;  
  22.     }  
根据定位返回的城市信息,获取城市天气代号代码:

[java]  view plain  copy
  1. package com.example.util;  
  2.   
  3. public class LocalCity {  
  4.       
  5.     public static String cityIds = "北京:101010100朝阳:101010300顺义:101010400" +  
  6.             "怀柔:101010500通州:101010600昌平:101010700延庆:101010800丰台:101010900石景山:101011000大兴:101011100房山:101011200密云:101011300门头沟:101011400平谷:101011500八达岭:101011600佛爷顶:101011700汤河口:101011800密云上甸子:101011900斋堂:101012000霞云岭:101012100北京城区:101012200海淀:101010200天津:101030100宝坻:101030300东丽:101030400西青:101030500北辰:101030600蓟县:101031400汉沽:101030800静海:101030900津南:101031000塘沽:101031100大港:101031200武清:101030200宁河:101030700上海:101020100宝山:101020300嘉定:101020500南汇:101020600浦东:101021300青浦:101020800松江:101020900奉贤:101021000崇明:101021100徐家汇:101021200闵行:101020200金山:101020700石家庄:101090101张家口:101090301承德:101090402唐山:101090501秦皇岛:101091101沧州:101090701衡水:101090801邢台:101090901邯郸:101091001保定:101090201廊坊:101090601郑州:101180101新乡:101180301许昌:101180401平顶山:101180501信阳:101180601南阳:101180701开封:101180801洛阳:101180901商丘:101181001焦作:101181101鹤壁:101181201濮阳:101181301周口:101181401漯河:101181501驻马店:101181601三门峡:101181701济源:101181801安阳:101180201合肥:101220101芜湖:101220301淮南:101220401马鞍山:101220501安庆:101220601宿州:101220701阜阳:101220801亳州:101220901黄山:101221001滁州:101221101淮北:101221201铜陵:101221301宣城:101221401六安:101221501巢湖:101221601池州:101221701蚌埠:101220201杭州:101210101舟山:101211101湖州:101210201嘉兴:101210301金华:101210901绍兴:101210501台州:101210601温州:101210701丽水:101210801衢州:101211001宁波:101210401重庆:101040100合川:101040300南川:101040400江津:101040500万盛:101040600渝北:101040700北碚:101040800巴南:101040900长寿:101041000黔江:101041100万州天城:101041200万州龙宝:101041300涪陵:101041400开县:101041500城口:101041600云阳:101041700巫溪:101041800奉节:101041900巫山:101042000潼南:101042100垫江:101042200梁平:101042300忠县:101042400石柱:101042500大足:101042600荣昌:101042700铜梁:101042800璧山:101042900丰都:101043000武隆:101043100彭水:101043200綦江:101043300酉阳:101043400秀山:101043600沙坪坝:101043700永川:101040200福州:101230101泉州:101230501漳州:101230601龙岩:101230701晋江:101230509南平:101230901厦门:101230201宁德:101230301莆田:101230401三明:101230801兰州:101160101平凉:101160301庆阳:101160401武威:101160501金昌:101160601嘉峪关:101161401酒泉:101160801天水:101160901武都:101161001临夏:101161101合作:101161201白银:101161301定西:101160201张掖:101160701广州:101280101惠州:101280301梅州:101280401" +  
  7.             "汕头:101280501深圳:101280601珠海:101280701佛山:101280800肇庆:101280901湛江:101281001江门:101281101河源:101281201清远:101281301云浮:101281401潮州:101281501东莞:101281601中山:101281701阳江:101281801揭阳:101281901茂名:101282001汕尾:101282101韶关:101280201南宁:101300101柳州:101300301来宾:101300401桂林:101300501梧州:101300601防城港:101301401贵港:101300801玉林:101300901百色:101301001钦州:101301101河池:101301201北海:101301301崇左:101300201贺州:101300701贵阳:101260101安顺:101260301都匀:101260401兴义:101260906铜仁:101260601毕节:101260701六盘水:101260801遵义:101260201凯里:101260501昆明:101290101红河:101290301文山:101290601玉溪:101290701楚雄:101290801普洱:101290901昭通:101291001临沧:101291101怒江:101291201香格里拉:101291301丽江:101291401德宏:101291501景洪:101291601大理:101290201曲靖:101290401保山:101290501呼和浩特:101080101乌海:101080301集宁:101080401通辽:101080501阿拉善左旗:101081201鄂尔多斯:101080701临河:101080801锡林浩特:101080901呼伦贝尔:101081000乌兰浩特:101081101包头:101080201赤峰:101080601南昌:101240101上饶:101240301抚州:101240401宜春:101240501鹰潭:101241101赣州:101240701景德镇:101240801萍乡:101240901新余:101241001九江:101240201吉安:101240601武汉:101200101黄冈:101200501荆州:101200801宜昌:101200901恩施:101201001十堰:101201101神农架:101201201随州:101201301荆门:101201401天门:101201501仙桃:101201601潜江:101201701襄樊:101200201鄂州:101200301孝感:101200401黄石:101200601咸宁:101200701成都:101270101自贡:101270301绵阳:101270401南充:101270501达州:101270601遂宁:101270701广安:101270801巴中:101270901泸州:101271001宜宾:101271101内江:101271201资阳:101271301乐山:101271401眉山:101271501凉山:101271601雅安:101271701甘孜:101271801阿坝:101271901德阳:101272001广元:101272101攀枝花:101270201银川:101170101中卫:101170501固原:101170401石嘴山:101170201吴忠:101170301西宁:101150101黄南:101150301海北:101150801果洛:101150501玉树:101150601海西:101150701海东:101150201海南:101150401济南:101120101潍坊:101120601临沂:101120901菏泽:101121001滨州:101121101东营:101121201威海:101121301枣庄:101121401日照:101121501莱芜:101121601聊城:101121701青岛:101120201淄博:101120301德州:101120401烟台:101120501济宁:101120701泰安:101120801西安:101110101延安:101110300榆林:101110401铜川:101111001商洛:101110601安康:101110701汉中:101110801宝鸡:101110901咸阳:101110200渭南:101110501太原:101100101临汾:101100701运城:101100801朔州:101100901忻州:101101001长治:101100501大同:101100201阳泉:101100301晋中:101100401晋城:101100601吕梁:101101100乌鲁木齐:101130101石河子:101130301昌吉:101130401吐鲁番:101130501库尔勒:101130601阿拉尔:101130701阿克苏:101130801喀什:101130901伊宁:101131001塔城:101131101哈密:101131201和田:101131301阿勒泰:101131401阿图什:101131501博乐:101131601克拉玛依:101130201拉萨:101140101山南:101140301阿里:101140701昌都:101140501那曲:101140601日喀则:101140201林芝:101140401台北县:101340101高雄:101340201台中:101340401海口:101310101三亚:101310201东方:101310202临高:101310203澄迈:101310204儋州:101310205昌江:101310206白沙:101310207琼中:101310208定安:101310209屯昌:101310210琼海:101310211文昌:101310212保亭:101310214万宁:101310215陵水:101310216西沙:101310217南沙岛:101310220乐东:101310221五指山:101310222琼山:101310102长沙:101250101株洲:101250301衡阳:101250401郴州:101250501常德:101250601益阳:101250700娄底:101250801邵阳:101250901岳阳:101251001张家界:101251101怀化:101251201黔阳:101251301永州:101251401吉首:101251501湘潭:101250201南京:101190101镇江:101190301苏州:101190401南通:101190501扬州:101190601宿迁:101191301徐州:101190801淮安:101190901连云港:101191001常州:101191101泰州:101191201无锡:101190201盐城:101190701哈尔滨:101050101牡丹江:101050301佳木斯:101050401绥化:101050501黑河:101050601双鸭山:101051301伊春:101050801大庆:101050901七台河:101051002鸡西:101051101鹤岗:101051201齐齐哈尔:101050201大兴安岭:101050701长春:101060101延吉:101060301四平:101060401白山:101060901白城:101060601辽源:101060701松原:101060801吉林:101060201通化:101060501沈阳:101070101鞍山:101070301抚顺:101070401本溪:101070501丹东:101070601葫芦岛:101071401营口:101070801阜新:101070901辽阳:101071001铁岭:101071101朝阳:101071201盘锦:101071301大连:101070201锦州:101070701 ";  
  8.       
  9.     private String city = "郑州";  
  10.   
  11.     public String getCity() {  
  12.         return city;  
  13.     }  
  14.   
  15.     public void setCity(String city) {  
  16.         this.city = city;  
  17.     }  
  18.     /** 
  19.      * 通过城市的名称获取对应的城市id,如果不存在则返回 null 
  20.      *  
  21.      * @param name 
  22.      *            城市名称 
  23.      * @return cityid 
  24.      * */  
  25.     public static String getCityIdByName(String name) {  
  26.         String cityId = null;  
  27.   
  28.         int startIndex = LocalCity.cityIds.indexOf(name) + name.length() + 1;// 开始截取的位置  
  29.         if (startIndex == -1) {  
  30.             return null;  
  31.         }  
  32.   
  33.         cityId = LocalCity.cityIds.trim().substring(startIndex, startIndex + 9);  
  34.   
  35.         return cityId;  
  36.     }  
  37. }  

源码地址:


http://download.csdn.net/detail/xiaorenwu1206/8067905

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值