JavaScript获取本地城市和天气预报实现

转自:https://blog.csdn.net/hehexiaoxia/article/details/61918553

1、获取城市接口,新浪的接口相对简单。

URL:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js

结果:

[javascript]   view plain  copy
  1. var remote_ip_info = {"ret":1,"start":-1,"end":-1,"country":"\u4e2d\u56fd","province":"\u5317\u4eac","city":"\u5317\u4eac","district":"","isp":"","type":"","desc":""};  


2、获取天气预报接口,因为baidu的接口有雾霾指数,所以选择了它。

URL:http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=EGgzZ22dsboWQEcPQ6KDQLknQd3YkkkP

响应结果:

[javascript]   view plain  copy
  1. {"error":0,"status":"success","date":"2017-03-13","results":[{"currentCity":"北京","pm25":"28","index":[{"title":"穿衣","zs":"较冷","tipt":"穿衣指数","des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,温度适宜,是个好天气哦。这样的天气适宜旅游,您可以尽情地享受大自然的风光。"},{"title":"感冒","zs":"较易发","tipt":"感冒指数","des":"昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。"},{"title":"运动","zs":"较不宜","tipt":"运动指数","des":"天气较好,但考虑天气寒冷,推荐您进行各种室内运动,若在户外运动请注意保暖并做好准备活动。"},{"title":"紫外线强度","zs":"中等","tipt":"紫外线强度指数","des":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"}],"weather_data":[{"date":"周一 03月13日 (实时:14℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"南风微风","temperature":"14 ~ 2℃"},{"date":"周二","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"晴转多云","wind":"南风微风","temperature":"15 ~ 2℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"南风微风","temperature":"16 ~ 4℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"多云转阴","wind":"南风微风","temperature":"17 ~ 5℃"}]}]}  


3、简单应用

[html]   view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <meta http-equiv="Content-Type" content="text/html;charset=utf-8">  
  6.     <meta http-equiv="U-XA-Compatible" content="IE=edge,chrome=1">  
  7.     <meta http-equiv="X-UA-Compatible" content="IE=9">  
  8.     <meta name="viewport" content="width=device-width,initial-scale=1">  
  9.     <meta name="renderer" content="webkit|ie-comp|ie-stand">  
  10.     <script type="text/javascript" src="/static/js/1.4.3/jquery.min.js"></script>  
  11. </head>  
  12.   
  13. <body>  
  14.     <img id="cvsToday" src="">  
  15.     <label id="lblWeather">--</label>  
  16.     <label id="lblTemperature">--</label>  
  17.     <label id="lblCurTemp">--</label>  
  18.     <script type="text/javascript">  
  19.         // 获取城市  
  20.         var cityUrl = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';  
  21.         $.getScript(  
  22.             cityUrl,  
  23.             function() {  
  24.                 var city = remote_ip_info.city; // 获取城市  
  25.   
  26.                 // 获取天气预报  
  27.                 $.ajax({  
  28.                     url: "http://api.map.baidu.com/telematics/v3/weather?location=" + city + "&output=json&ak=EGgzZ22dsboWQEcPQ6KDQLknQd3YkkkP",  
  29.                     type: "get",  
  30.                     dataType: "jsonp",  
  31.                     scriptCharset: "gbk",  
  32.                     success: function(baiduTQ) {  
  33.                         try {  
  34.                             if (baiduTQ == null || baiduTQ.error != 0 || baiduTQ.status != "success" || baiduTQ.results.count == 0) {  
  35.                                 document.getElementById("lblTemperature").innerHTML = "--";  
  36.                                 document.getElementById("lblWeather").innerHTML = "--";  
  37.                                 document.getElementById("lblCurTemp").innerHTML = "--";  
  38.                                 return;  
  39.                             }  
  40.                             if (baiduTQ.results[0].weather_data.length > 0) {  
  41.                                 var data = baiduTQ.results[0].weather_data[0];  
  42.                                 var split = data.date.split(":");  
  43.                                 document.getElementById("lblTemperature").innerHTML = city + "[" + data.temperature + "]";  
  44.                                 document.getElementById("lblWeather").innerHTML = data.weather;  
  45.                                 var curTemp = split.pop();  
  46.                                 document.getElementById("lblCurTemp").innerHTML = curTemp.substring(0, curTemp.length - 1);  
  47.   
  48.                                 var imgPath = data.dayPictureUrl;  
  49.                                 document.getElementById("cvsToday").src = imgPath;  
  50.                             }  
  51.                         } catch (err) {  
  52.                             alert(err)  
  53.                         }  
  54.                     }  
  55.                 });  
  56.             });  
  57.     </script>  
  58. </body>  
  59.   
  60. </html>  
结果展示:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值