日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
jQ、Yahoo API和HTML 5開發(fā)天氣預(yù)報應(yīng)用

在線演示  本地下載

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比鳳陽網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式鳳陽網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋鳳陽地區(qū)。費(fèi)用合理售后完善,十余年實體公司更值得信賴。

今天我們介紹來自tutorialzine的一個HTML5/jQuery/Yahoo API的開發(fā)教程,在這篇文章中我們將介紹如何使用HTML5的Geolocation,jQuery和YahooAPI來開發(fā)一個天氣預(yù)報web應(yīng)用。 如果你不熟悉HTML5的Geolocation(地理位置服務(wù)),請參考我們的HTML5教程: HTML5 Geolocation。

首先你需要得到Y(jié)ahoo API的API key,你可以通過如下地址取得對應(yīng)的API key:https://developer.apps.yahoo.com/dashboard/createKey.html

以上創(chuàng)建過程中會要求你輸入相關(guān)應(yīng)用地址等信息。創(chuàng)建成功后,你可以得到APPID。

主要思路

在這個教程中,我們主要思路如下:

使用Geolocation取得用戶的地理位置信息

然后,使用yahoo的 PlaceFinder API,來通過經(jīng)緯度來找到具體地點,例如,城市或者國家。其中包括了woeid,這個用來在天氣預(yù)報應(yīng)用中找到國家。

最后,我們將調(diào)用yahoo的Weather API來取得天氣。

web應(yīng)用代碼

#p#

HTML

 
 
 
 
  1.  
  2.  
  3.      
  4.          
  5.         Weather Forecast with jQuery & Yahoo APIs 
  6.           
  7.          
  8.          
  9.           
  10.          
  11.          
  12.           
  13.          
  14.      
  15.       
  16.      
  17.  
  18.         
     
  19.             

    Weather Forecast

     
  20.          
  21.           
  22.          
  23.  
  24.              
  25.                  
  26.              
  27.               
  28.             Previous 
  29.             Next 
  30.               
  31.         
 
  •           
  •         

     
  •           
  •         
  •  
  •           
  •         
     
  •             

    Tutorial: Weather Forecast with jQuery & Yahoo APIs

     
  •             Head on to Tutorialzine to download this example 
  •          
  •           
  •          
  •          
  •          
  •           
  •      
  •  
  • #p#

    Javascript

     
     
     
     
    1. $(function(){  
    2.       
    3.     /* Configuration */ 
    4.       
    5.     var APPID = 'fa2pT26k';        // Your Yahoo APP id  
    6.     var DEG = 'c';        // c for celsius, f for fahrenheit  
    7.       
    8.     // Mapping the weather codes returned by Yahoo's API  
    9.     // to the correct icons in the img/icons folder  
    10.       
    11.     var weatherIconMap = [  
    12.         'storm', 'storm', 'storm', 'lightning', 'lightning', 'snow', 'hail', 'hail',  
    13.         'drizzle', 'drizzle', 'rain', 'rain', 'rain', 'snow', 'snow', 'snow', 'snow',  
    14.         'hail', 'hail', 'fog', 'fog', 'fog', 'fog', 'wind', 'wind', 'snowflake',  
    15.         'cloud', 'cloud_moon', 'cloud_sun', 'cloud_moon', 'cloud_sun', 'moon', 'sun',  
    16.         'moon', 'sun', 'hail', 'sun', 'lightning', 'lightning', 'lightning', 'rain',  
    17.         'snowflake', 'snowflake', 'snowflake', 'cloud', 'rain', 'snow', 'lightning' 
    18.     ];  
    19.       
    20.     var weatherDiv = $('#weather'),  
    21.         scroller = $('#scroller'),  
    22.         location = $('p.location');  
    23.       
    24.     // Does this browser support geolocation?  
    25.     if (navigator.geolocation) {  
    26.         navigator.geolocation.getCurrentPosition(locationSuccess, locationError);  
    27.     }  
    28.     else{  
    29.         showError("Your browser does not support Geolocation!");  
    30.     }  
    31.       
    32.     // Get user's location, and use Yahoo's PlaceFinder API  
    33.     // to get the location name, woeid and weather forecast  
    34.       
    35.     function locationSuccess(position) {  
    36.         var lat = position.coords.latitude;  
    37.         var lon = position.coords.longitude;  
    38.  
    39.         // Yahoo's PlaceFinder API http://developer.yahoo.com/geo/placefinder/  
    40.         // We are passing the R gflag for reverse geocoding (coordinates to place name)  
    41.         var geoAPI = 'http://where.yahooapis.com/geocode?location='+lat+','+lon+'&flags=J&gflags=R&appid='+APPID;  
    42.           
    43.         // Forming the query for Yahoo's weather forecasting API with YQL  
    44.         // http://developer.yahoo.com/weather/  
    45.           
    46.         var wsql = 'select * from weather.forecast where woeid=WID and u="'+DEG+'"',  
    47.             weatherYQL = 'http://query.yahooapis.com/v1/public/yql?q='+encodeURIComponent(wsql)+'&format=json&callback=?',  
    48.             code, city, results, woeid;  
    49.           
    50.         if (window.console && window.console.info){  
    51.             console.info("Coordinates: %f %f", lat, lon);  
    52.         }  
    53.           
    54.         // Issue a cross-domain AJAX request (CORS) to the GEO service.  
    55.         // Not supported in Opera and IE.  
    56.         $.getJSON(geoAPI, function(r){  
    57.              
    58.             if(r.ResultSet.Found == 1){  
    59.                   
    60.                 results = r.ResultSet.Results;  
    61.                 city = results[0].city;  
    62.                 code = results[0].statecode || results[0].countrycode;  
    63.           
    64.                 // This is the city identifier for the weather API  
    65.                 woeid = results[0].woeid;  
    66.       
    67.                 // Make a weather API request:  
    68.                 $.getJSON(weatherYQL.replace('WID',woeid), function(r){  
    69.                       
    70.                     if(r.query && r.query.count == 1){  
    71.                           
    72.                         // Create the weather items in the #scroller UL  
    73.                           
    74.                         var item = r.query.results.channel.item.condition;  
    75.                           
    76.                         if(!item){  
    77.                             showError("We can't find weather information about your city!");  
    78.                             if (window.console && window.console.info){  
    79.                                 console.info("%s, %s; woeid: %d", city, code, woeid);  
    80.                             }  
    81.                               
    82.                             return false;  
    83.                         }  
    84.                           
    85.                         addWeather(item.code, "Now", item.text + ' '+item.temp+'°'+DEG+'');  
    86.                           
    87.                         for (var i=0;i<2;i++){  
    88.                             item = r.query.results.channel.item.forecast[i];  
    89.                             addWeather(  
    90.                                 item.code,   
    91.                                 item.day +' '+item.date.replace('\d+$','')+'',  
    92.                                 item.text + ' '+item.low+'°'+DEG+' / '+item.high+'°'+DEG+'
    93.                             );  
    94.                         }  
    95.                           
    96.                         // Add the location to the page  
    97.                         location.html(city+', '+code+'');  
    98.                           
    99.                         weatherDiv.addClass('loaded');  
    100.                           
    101.                         // Set the slider to the first slide  
    102.                         showSlide(0);  
    103.                      
    104.                     }  
    105.                     else {  
    106.                         showError("Error retrieving weather data!");  
    107.                     }  
    108.                 });  
    109.           
    110.             }  
    111.               
    112.         }).error(function(){  
    113.             showError("Your browser does not support CORS requests!");  
    114.         });  
    115.          
    116.     }  
    117.       
    118.     function addWeather(code, day, condition){  
    119.           
    120.         var markup = '
    121. '+  
    122.             ''+  
    123.             ' '+ day +'

       '+ condition +  
    124.             '

    125. ';  
    126.           
    127.         scroller.append(markup);  
    128.     }  
    129.       
    130.     /* Handling the previous / next arrows */ 
    131.       
    132.     var currentSlide = 0;  
    133.     weatherDiv.find('a.previous').click(function(e){  
    134.         e.preventDefault();  
    135.         showSlide(currentSlide-1);  
    136.     });  
    137.       
    138.     weatherDiv.find('a.next').click(function(e){  
    139.         e.preventDefault();  
    140.         showSlide(currentSlide+1);  
    141.     });  
    142.       
    143.       
    144.     function showSlide(i){  
    145.         var items = scroller.find('li');  
    146.           
    147.         if (i >= items.length || i < 0 || scroller.is(':animated')){  
    148.             return false;  
    149.         }  
    150.           
    151.         weatherDiv.removeClass('first last');  
    152.           
    153.         if(i == 0){  
    154.             weatherDiv.addClass('first');  
    155.         }  
    156.         else if (i == items.length-1){  
    157.             weatherDiv.addClass('last');  
    158.         }  
    159.           
    160.         scroller.animate({left:(-i*100)+'%'}, function(){  
    161.             currentSlide = i;  
    162.         });  
    163.     }  
    164.       
    165.     /* Error handling functions */ 
    166.       
    167.     function locationError(error){  
    168.         switch(error.code) {  
    169.             case error.TIMEOUT:  
    170.                 showError("A timeout occured! Please try again!");  
    171.                 break;  
    172.             case error.POSITION_UNAVAILABLE:  
    173.                 showError('We can\'t detect your location. Sorry!');  
    174.                 break;  
    175.             case error.PERMISSION_DENIED:  
    176.                 showError('Please allow geolocation access for this to work.');  
    177.                 break;  
    178.             case error.UNKNOWN_ERROR:  
    179.                 showError('An unknown error occured!');  
    180.                 break;  
    181.         }  
    182.           
    183.     }  
    184.       
    185.     function showError(msg){  
    186.         weatherDiv.addClass('error').html(msg);  
    187.     }  
    188.  
    189. }); 

    搞定!具體演示請參考在線Demo,希望大家喜歡這個web應(yīng)用!


    文章名稱:jQ、Yahoo API和HTML 5開發(fā)天氣預(yù)報應(yīng)用
    轉(zhuǎn)載來源:http://www.dlmjj.cn/article/dppiheg.html