zoukankan      html  css  js  c++  java
  • Jquery和雅虎的YQL服务实现天气预报功能!

    可以直接使用http://www.tianqi.com/plugin/#tjdm定制天气

    很多外部网站都有天气预报功能,对于很多企业内部的门户也需要有天气预报功能,但实现天气预报的功能和方式确有所差异,本文介绍一个利用Jquery和雅虎的YQL服务实现天气预报功能,不涉及任何后端开发代码(如.Net,JAVA等),并在本人之前开发的移动OA网站上使用。目前最权威的天气预报数据是中国天气网(http://www.weather.com.cn/),因为这个是官方提供的气象数据,除了商业的增值服务外,还提供了免费的以JSON数据格式返回的气象数据,以查看杭州的天气数据为例,可以输入以下地址:http://m.weather.com.cn/data/101210101.html ,返回的JSON数据格式如下图:

     

        YQL服务可以实现对网上不同数据源的query,filter,combine(查询,过滤,合并),提供类似SQL,具体地址如下:http://developer.yahoo.com/yql/console/ 。当实施查询的时候,YQL服务就会访问网络上的数据源,传输数据,返回XML或者JSON形式的数据结果。YQL可以使用许多类型的数据源,包括Yahoo!Web services 或者其他的网络服务,和网络数据类型例如:HTML, XML, RSS,和Atom。

           因此可以通过两者的结合使用,完成天气预报功能的开发,具体JS代码如下:

    复制代码
              function getWeather() {
               
                 $.getJSON("http://query.yahooapis.com/v1/public/yql", {
                     q: "select * from json where url="http://m.weather.com.cn/data/101210101.html"",
                    format: "json"
                }, function (data) {
                    if (data.query.results) {
                        //$("#content").text(JSON.stringify(data.query.results));
                        var J_data = JSON.parse(JSON.stringify(data.query.results));
                         //alert(J_data.weatherinfo.city);
                           $("#content").append("<p>"+J_data.weatherinfo.city+"天气预报(数据来源中国天气网)"+"</p>");
                         $("#content").append("<p>"+J_data.weatherinfo.date_y+"&nbsp;"+J_data.weatherinfo.week+"&nbsp;"+J_data.weatherinfo.temp1+"&nbsp;"+J_data.weatherinfo.weather1+"&nbsp;"+J_data.weatherinfo.wind1+"&nbsp;"+J_data.weatherinfo.index+"&nbsp;"+J_data.weatherinfo.index_d+"</p>");
                         var t= J_data.weatherinfo.date_y;
                         t=t.replace("年","/");
                         t=t.replace("月","/");
                         t=t.replace("日","");
    
                         var tdy = new Date(t);  
                         
                         var t2 = new Date();       
                        
                       
                          t2.setDate(tdy.getDate()+1);
                          
                        
    
                          $("#content").append("<p>"+ t2.Format("yyyy年MM月dd日")+"&nbsp;"+getweekdays(t2)+"&nbsp;"+J_data.weatherinfo.temp2+"&nbsp;"+J_data.weatherinfo.weather2+"&nbsp;"+J_data.weatherinfo.wind2+"</p>");
                          
                           var t3 = new Date();
                       
                          t3.setDate(tdy.getDate()+2);
                          $("#content").append("<p>"+t3.Format("yyyy年MM月dd日")+"&nbsp;"+getweekdays(t3)+"&nbsp;"+J_data.weatherinfo.temp3+"&nbsp;"+J_data.weatherinfo.weather3+"&nbsp;"+J_data.weatherinfo.wind3+"</p>");
                                        
                          var t4 = new Date();
                       
                          t4.setDate(tdy.getDate()+3);
                          $("#content").append("<p>"+t4.Format("yyyy年MM月dd日")+"&nbsp;"+getweekdays(t4)+"&nbsp;"+J_data.weatherinfo.temp4+"&nbsp;"+J_data.weatherinfo.weather4+"&nbsp;"+J_data.weatherinfo.wind4+"</p>");
                                  
                          var t5 = new Date();
                       
                          t5.setDate(tdy.getDate()+4);
                          $("#content").append("<p>"+t5.Format("yyyy年MM月dd日")+"&nbsp;"+getweekdays(t5)+"&nbsp;"+J_data.weatherinfo.temp5+"&nbsp;"+J_data.weatherinfo.weather5+"&nbsp;"+J_data.weatherinfo.wind5+"</p>");
    
                          var t6 = new Date();
                       
                          t6.setDate(tdy.getDate()+5);
                          $("#content").append("<p>"+t6.Format("yyyy年MM月dd日")+"&nbsp;"+getweekdays(t6)+"&nbsp;"+J_data.weatherinfo.temp6+"&nbsp;"+J_data.weatherinfo.weather6+"&nbsp;"+J_data.weatherinfo.wind6+"</p>");
    
    
    
                         //alert(getweekdays(t2)); 
    
                    } else {
                         $("#content").text('no such code: ' + code);
                     }
                 });
                             
              //$.getJSON("http://m.weather.com.cn/data/101210101.html", null, function(json) { alert(json); });             
               
            }
    
            function getweekdays(datey)
            {
               if(datey.getDay()==0)
               {
                 return "星期日";
               }
               else if(datey.getDay()==1)
               {
                  return "星期一";
               }
               else if(datey.getDay()==2)
               {
                  return "星期二";
               }
               else if(datey.getDay()==3)
               {
                  return "星期三";
               }
               else if(datey.getDay()==4)
               {
                  return "星期四";
               }
               else if(datey.getDay()==5)
               {
                  return "星期五";
               }
               else if(datey.getDay()==6)
               {
                  return "星期六";
               }
    
    
    
            }
    复制代码

              最终实现的效果,如下图:

            

  • 相关阅读:
    两个Stirng[]拼接成一个数组
    Visual code 常用快捷键
    mysql 中的分页limit
    移动端web轮播图插件swiper,功能很强大
    array_splice()函数 ,删除数组中的某个值
    Github简单的上传和修改
    PHP数组在循环的时候修改本身的值
    IP定位,天气接口
    使用百度翻译的API接口
    laravel 队列
  • 原文地址:https://www.cnblogs.com/yxlblogs/p/3365238.html
Copyright © 2011-2022 走看看