ads

Monday, December 3, 2012

How to display current weather via yahoo service

To get specific city weather or country you can find here
example: in the search box insert  "addis ababa ethiopia" and its will retrieve the area code you can see in the url

http://www.weather.com/weather/today/Addis%20Ababa+Ethiopia+ETXX0001

css:

Build a table to display weather image.

<style>
<table border="0" width="100%">
<tr id="WtitleImage"></tr>
<tr id="Wc"></tr></table>
<div id="WIcon" />
</style>

Java script:


<script type="text/javascript" >

// to show result in celsius set u=c
   to show in  fahrenheit set u=f

//build a query for yahoo service
var u="c";
var loc="ETXX0001";
var query = "SELECT item.condition \
        FROM weather.forecast \
        WHERE location='" + loc + "' AND u='" + u + "'",    url = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(query) + '&format=json';
    $.ajax({
        url: url,
        dataType: 'jsonp', // this automatically disables cache too
        success: function(data) {
            var info = data.query.results.channel.item.condition;
            $('#WIcon').css({
                backgroundPosition: '-' + (61 * info.code) + 'px 0'
            }).attr({
                title: info.text
                            });

// append to table
                        $('#WtitleImage').append('<td>Addis Ababa Ethiopia</br><img src="http://l.yimg.com/a/i/us/we/52/' + info.code + '.gif" width="34" height="34" title="' + info.text + '" /></td>');
                         $('#Wc').append('<td>'+info.temp + '&deg;' +(u.toUpperCase())+'</td>');        }
    });
</script>

No comments:

Post a Comment