หน้าเว็บ

วันพฤหัสบดีที่ 7 มิถุนายน พ.ศ. 2555

basic weather api


weather.js
function write(str){
    document.write(str);
} 
function writeln(str){
    write(str);
    write("<br/>");
}
function image(str){
    writeln("<img src='" + str + "'/>");
}
jQuery(document).ready(function($) {
    $.ajax({
        url: "http://api.wunderground.com/api/YOUR_API_KEY/geolookup/conditions/forecast/lang:TH/q/Bangkok.json",
        dataType: "jsonp",
        success: function(jsonResult) {
            writeln("เมือง : " + jsonResult['location']['city']);
            writeln("ประเทศ : " + jsonResult['location']['country_name']);
            writeln("อักษร ประเทศ : " + jsonResult['location']['country']);
            writeln("ละติจูด : " + jsonResult['location']['lat']);
            writeln("ลองจิจูด : " + jsonResult['location']['lon']);
            writeln("วันนี้วัน : " + jsonResult['current_observation']['local_time_rfc822']);
            writeln("อุณหภูมิ : " + jsonResult['current_observation']['temperature_string']);
            writeln("สภาพอากาศ : " + jsonResult['current_observation']['weather']);
            writeln("-----------------------------------------------------------");

            $.each(jsonResult['forecast']['txt_forecast']['forecastday'],function(index, value){
                writeln("วัน : " + value['title']);
                image(value['icon_url']);
                writeln("พยากรณ์ : " + value['fcttext_metric']);
                writeln("-----------------------------------------------------------");
            });
        }
    });
});
weather.html
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
        <script type="text/javascript" src="js/weather.js"></script>
    </head>
    <body>
    </body>
</html>
เรียนรู้ได้ที่ : http://www.wunderground.com/

นี่คือ json pattern ที่รับมาได้ครับ
{
    "response": 
    {
        "version": "0.1",
        "termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
        "features": 
        {
            "geolookup": 1,
            "conditions": 1,
            "forecast": 1
        }
    }, 
    "location": 
    {
        "type":"INTLCITY",
        "country":"TH",
        "country_iso3166":"TH",
        "country_name":"ประเทศไทย",
        "state":"",
        "city":"กรุงเทพมหานคร",
        "tz_short":"ICT",
        "tz_long":"Asia/Bangkok",
        "lat":"13.75000000",
        "lon":"100.51667023",
        "zip":"00000",
        "magic":"16",
        "wmo":"48456",
        "l":"/q/zmw:00000.16.48456",
        "requesturl":"global/stations/48456.html",
        "wuiurl":"http://www.wunderground.com/global/stations/48456.html",
        "nearby_weather_stations": 
        {
            "airport": 
            {
                "station": 
                [ 
                    {
                        "city":"Don Muang", 
                        "state":"", 
                        "country":"Thailand", 
                        "icao":"VTBD", 
                        "lat":"13.91666698", 
                        "lon":"100.59999847" 
                    },
                    { 
                        "city":"Bangkok (Don Mueang)", 
                        "state":"", 
                        "country":"TH", 
                        "icao":"VTBD", 
                        "lat":"13.90999985", 
                        "lon":"100.59999847" 
                    },
                    { 
                        "city":"Bangkok (Suvarnabhumi)", 
                        "state":"", 
                        "country":"TH", 
                        "icao":"VTBS", 
                        "lat":"13.36999989", 
                        "lon":"100.98000336" 
                    }
                ]
            },
            "pws": 
            {
                "station": 
                [
                    {
                        "neighborhood":"Sala Thammasop",
                        "city":"Thawi Watthana",
                        "state":"Bangkok",
                        "country":"THAILAND",
                        "id":"IBANGKOK24",
                        "distance_km":14,
                        "distance_mi":8
                    }
                ]
            }
        }
    }, 
    "current_observation": 
    {
        "image": 
        {
            "url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
            "title":"Weather Underground",
            "link":"http://www.wunderground.com"
        },
        "display_location": 
        {
            "full":"กรุงเทพมหานคร, ประเทศไทย",
            "city":"กรุงเทพมหานคร",
            "state":"",
            "state_name":"ประเทศไทย",
            "country":"TH",
            "country_iso3166":"TH",
            "zip":"00000",
            "latitude":"13.75000000",
            "longitude":"100.51667023",
            "elevation":"11.00000000"
        },
        "observation_location": 
        {
            "full":"Sala Thammasop, Thawi Watthana, Bangkok",
            "city":"Sala Thammasop, Thawi Watthana",
            "state":"Bangkok",
            "country":"THAILAND",
            "country_iso3166":"TH",
            "latitude":"13.793689",
            "longitude":"100.394722",
            "elevation":"3 ft"
        },
        "estimated": 
        {
  
        },
        "station_id":"IBANGKOK24",
        "observation_time":"Last Updated on มิถุนายน 8, 8:39 PM ICT",
        "observation_time_rfc822":"Fri, 08 Jun 2012 20:39:14 +0700",
        "observation_epoch":"1339162754",
        "local_time_rfc822":"Fri, 08 Jun 2012 20:39:57 +0700",
        "local_epoch":"1339162797",
        "local_tz_short":"ICT",
        "local_tz_long":"Asia/Bangkok",
        "local_tz_offset":"+0700",
        "weather":"มีเมฆคลุมเป็นส่วนใหญ่",
        "temperature_string":"84.8 F (29.3 C)",
        "temp_f":84.8,
        "temp_c":29.3,
        "relative_humidity":"74%",
        "wind_string":"From the ทางทิศใต้และทิศตะวันตกเฉียงใต้ at 1.8 MPH Gusting to 4.7 MPH",
        "wind_dir":"ทางทิศใต้และทิศตะวันตกเฉียงใต้",
        "wind_degrees":203,
        "wind_mph":1.8,
        "wind_gust_mph":"4.7",
        "wind_kph":2.9,
        "wind_gust_kph":"7.6",
        "pressure_mb":"1003",
        "pressure_in":"29.62",
        "pressure_trend":"0",
        "dewpoint_string":"76 F (24 C)",
        "dewpoint_f":76,
        "dewpoint_c":24,
        "heat_index_string":"94 F (34 C)",
        "heat_index_f":94,
        "heat_index_c":34,
        "windchill_string":"NA",
        "windchill_f":"NA",
        "windchill_c":"NA",
        "feelslike_string":"94 F (29.3 C)",
        "feelslike_f":"94",
        "feelslike_c":"29.3",
        "visibility_mi":"6.2",
        "visibility_km":"10.0",
        "solarradiation":"",
        "UV":"0",
        "precip_1hr_string":"0.00 in ( 0 mm)",
        "precip_1hr_in":"0.00",
        "precip_1hr_metric":" 0",
        "precip_today_string":"0.00 in (0 mm)",
        "precip_today_in":"0.00",
        "precip_today_metric":"0",
        "icon":"mostlycloudy",
        "icon_url":"http://icons-ak.wxug.com/i/c/k/nt_mostlycloudy.gif",
        "forecast_url":"http://www.wunderground.com/global/stations/48456.html",
        "history_url":"http://www.wunderground.com/history/airport/IBANGKOK24/2012/6/8/DailyHistory.html",
        "ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=13.793689,100.394722"
    },
    "forecast":
    {
        "txt_forecast": 
        {
            "date":"7:00 AM ICT",
            "forecastday": 
            [
                {
                    "period":0,
                    "icon":"tstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/tstorms.gif",
                    "title":"วันศุกร์",
                    "fcttext":"พายุฟ้าคะนอง. สูงสุด 99F. Wind Speed: 10-15 mph. Wind Direction: ทางทิศใต้. Chance of Precip. 70%.",
                    "fcttext_metric":"พายุฟ้าคะนอง. สูงสุด 37C. Wind Speed: 15-25 km/h. Wind Direction: ทางทิศใต้. Chance of Precip. 70%.",
                    "pop":"70"
                },
                {
                    "period":1,
                    "icon":"cloudy",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/cloudy.gif",
                    "title":"วันศุกร์ตอนกลางคืน",
                    "fcttext":"พายุฟ้าคะนอง. ต่ำสุด 77F. Wind Speed: 10-15 mph. Wind Direction: ทางทิศใต้และทิศตะวันตกเฉียงใต้. Chance of Precip. 60%.",
                    "fcttext_metric":"พายุฟ้าคะนอง. ต่ำสุด 25C. Wind Speed: 15-20 km/h. Wind Direction: ทางทิศใต้และทิศตะวันตกเฉียงใต้. Chance of Precip. 60%.",
                    "pop":"60"
                },
                {
                    "period":2,
                    "icon":"chancetstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/chancetstorms.gif",
                    "title":"วันเสาร์",
                    "fcttext":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. สูงสุด 97F. Wind Speed: 10-15 mph. Wind Direction: ทางทิศใต้และทิศตะวันตกเฉียงใต้. Chance of Precip. 40%.",
                    "fcttext_metric":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. สูงสุด 36C. Wind Speed: 15-25 km/h. Wind Direction: ทางทิศใต้และทิศตะวันตกเฉียงใต้. Chance of Precip. 40%.",
                    "pop":"40"
                },
                {
                    "period":3,
                    "icon":"chancetstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/chancetstorms.gif",
                    "title":"วันเสาร์ตอนกลางคืน",
                    "fcttext":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. ต่ำสุด 79F. Wind Speed: 10-15 mph. Wind Direction: ทางทิศตะวันตกเฉียงใต้. Chance of Precip. 50%.",
                    "fcttext_metric":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. ต่ำสุด 26C. Wind Speed: 15-20 km/h. Wind Direction: ทางทิศตะวันตกเฉียงใต้. Chance of Precip. 50%.",
                    "pop":"50"
                },
                {
                    "period":4,
                    "icon":"chancetstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/chancetstorms.gif",
                    "title":"วันอาทิตย์",
                    "fcttext":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. สูงสุด 95F. Wind Speed: 10-15 mph. Wind Direction: ทางทิศใต้และทิศตะวันตกเฉียงใต้. Chance of Precip. 60%.",
                    "fcttext_metric":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. สูงสุด 35C. Wind Speed: 15-25 km/h. Wind Direction: ทางทิศใต้และทิศตะวันตกเฉียงใต้. Chance of Precip. 60%.",
                    "pop":"60"
                },
                {
                    "period":5,
                    "icon":"chancetstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/chancetstorms.gif",
                    "title":"วันอาทิตย์ตอนกลางคืน",
                    "fcttext":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. ต่ำสุด 77F. Wind Speed: 10-15 mph. Wind Direction: ทางทิศใต้และทิศตะวันตกเฉียงใต้. Chance of Precip. 50%.",
                    "fcttext_metric":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. ต่ำสุด 25C. Wind Speed: 15-20 km/h. Wind Direction: ทางทิศใต้และทิศตะวันตกเฉียงใต้. Chance of Precip. 50%.",
                    "pop":"50"
                },
                {
                    "period":6,
                    "icon":"chancetstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/chancetstorms.gif",
                    "title":"วันจันทร์",
                    "fcttext":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. สูงสุด 95F. Wind Speed: 10-15 mph. Wind Direction: ทางทิศตะวันตกเฉียงใต้. Chance of Precip. 30%.",
                    "fcttext_metric":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. สูงสุด 35C. Wind Speed: 15-20 km/h. Wind Direction: ทางทิศตะวันตกเฉียงใต้. Chance of Precip. 30%.",
                    "pop":"30"
                },
                {
                    "period":7,
                    "icon":"mostlycloudy",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif",
                    "title":"วันจันทร์ตอนกลางคืน",
                    "fcttext":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. ต่ำสุด 79F. Wind Speed: 5-15 mph. Wind Direction: ทางทิศตะวันตกเฉียงใต้. Chance of Precip. 20%.",
                    "fcttext_metric":"มีโอกาสเกิดพายุฝนฟ้าคะนอง. ต่ำสุด 26C. Wind Speed: 10-20 km/h. Wind Direction: ทางทิศตะวันตกเฉียงใต้. Chance of Precip. 20%.",
                    "pop":"20"
                }
            ]
        },
        "simpleforecast": 
        {
            "forecastday": 
            [
                {
                    "date":
                    {
                        "epoch":"1339167600",
                        "pretty":"10:00 PM ICT on June 08, 2012",
                        "day":8,
                        "month":6,
                        "year":2012,
                        "yday":159,
                        "hour":22,
                        "min":"00",
                        "sec":0,
                        "isdst":"0",
                        "monthname":"มิถุนายน",
                        "weekday_short":"Fri",
                        "weekday":"วันศุกร์",
                        "ampm":"PM",
                        "tz_short":"ICT",
                        "tz_long":"Asia/Bangkok"
                    },
                    "period":1,
                    "high": 
                    {
                        "fahrenheit":"99",
                        "celsius":"37"
                    },
                    "low": 
                    {
                        "fahrenheit":"77",
                        "celsius":"25"
                    },
                    "conditions":"พายุฟ้าคะนอง",
                    "icon":"tstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/tstorms.gif",
                    "skyicon":"cloudy",
                    "pop":70,
                    "qpf_allday": 
                    {
                        "in": 0.31,
                        "mm": 7.9
                    },
                    "qpf_day": 
                    {
                        "in": 0.14,
                        "mm": 3.6
                    },
                    "qpf_night": 
                    {
                        "in": 0.11,
                        "mm": 2.8
                    },
                    "snow_allday": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "snow_day": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "snow_night": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "maxwind": 
                    {
                        "mph": 14,
                        "kph": 22,
                        "dir": "ทางทิศตะวันตกเฉียงใต้",
                        "degrees": 231
                    },
                    "avewind": 
                    {
                        "mph": 10,
                        "kph": 16,
                        "dir": "ทางทิศตะวันตกเฉียงใต้",
                        "degrees": 219
                    },
                    "avehumidity": 69,
                    "maxhumidity": 84,
                    "minhumidity": 53
                },
                {
                    "date":
                    {
                        "epoch":"1339254000",
                        "pretty":"10:00 PM ICT on June 09, 2012",
                        "day":9,
                        "month":6,
                        "year":2012,
                        "yday":160,
                        "hour":22,
                        "min":"00",
                        "sec":0,
                        "isdst":"0",
                        "monthname":"มิถุนายน",
                        "weekday_short":"Sat",
                        "weekday":"วันเสาร์",
                        "ampm":"PM",
                        "tz_short":"ICT",
                        "tz_long":"Asia/Bangkok"
                    },
                    "period":2,
                    "high": 
                    {
                        "fahrenheit":"97",
                        "celsius":"36"
                    },
                    "low": 
                    {
                        "fahrenheit":"79",
                        "celsius":"26"
                    },
                    "conditions":"มีโอกาสเกิดพายุฝนฟ้าคะนอง",
                    "icon":"chancetstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/chancetstorms.gif",
                    "skyicon":"cloudy",
                    "pop":40,
                    "qpf_allday": 
                    {
                        "in": 0.06,
                        "mm": 1.5
                    },
                    "qpf_day": 
                    {
                        "in": 0.04,
                        "mm": 1.0
                    },
                    "qpf_night": 
                    {
                        "in": 0.04,
                        "mm": 1.0
                    },
                    "snow_allday": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "snow_day": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "snow_night": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "maxwind": 
                    {
                        "mph": 14,
                        "kph": 22,
                        "dir": "ทางทิศใต้และทิศตะวันตกเฉียงใต้",
                        "degrees": 197
                    },
                    "avewind": 
                    {
                        "mph": 11,
                        "kph": 18,
                        "dir": "ทางทิศใต้และทิศตะวันตกเฉียงใต้",
                        "degrees": 206
                    },
                    "avehumidity": 72,
                    "maxhumidity": 78,
                    "minhumidity": 52
                },
                {
                    "date":
                    {
                        "epoch":"1339340400",
                        "pretty":"10:00 PM ICT on June 10, 2012",
                        "day":10,
                        "month":6,
                        "year":2012,
                        "yday":161,
                        "hour":22,
                        "min":"00",
                        "sec":0,
                        "isdst":"0",
                        "monthname":"มิถุนายน",
                        "weekday_short":"Sun",
                        "weekday":"วันอาทิตย์",
                        "ampm":"PM",
                        "tz_short":"ICT",
                        "tz_long":"Asia/Bangkok"
                    },
                    "period":3,
                    "high": 
                    {
                        "fahrenheit":"95",
                        "celsius":"35"
                    },
                    "low": 
                    {
                        "fahrenheit":"77",
                        "celsius":"25"
                    },
                    "conditions":"มีโอกาสเกิดพายุฝนฟ้าคะนอง",
                    "icon":"chancetstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/chancetstorms.gif",
                    "skyicon":"mostlycloudy",
                    "pop":60,
                    "qpf_allday": 
                    {
                        "in": 0.39,
                        "mm": 9.9
                    },
                    "qpf_day": 
                    {
                        "in": 0.22,
                        "mm": 5.6
                    },
                    "qpf_night": 
                    {
                        "in": 0.15,
                        "mm": 3.8
                    },
                    "snow_allday": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "snow_day": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "snow_night": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "maxwind": 
                    {
                        "mph": 13,
                        "kph": 21,
                        "dir": "ทางทิศใต้และทิศตะวันตกเฉียงใต้",
                        "degrees": 203
                    },
                    "avewind": 
                    {
                        "mph": 11,
                        "kph": 18,
                        "dir": "ทางทิศใต้และทิศตะวันตกเฉียงใต้",
                        "degrees": 201
                    },
                    "avehumidity": 74,
                    "maxhumidity": 79,
                    "minhumidity": 50
                },
                {
                    "date":
                    {
                        "epoch":"1339426800",
                        "pretty":"10:00 PM ICT on June 11, 2012",
                        "day":11,
                        "month":6,
                        "year":2012,
                        "yday":162,
                        "hour":22,
                        "min":"00",
                        "sec":0,
                        "isdst":"0",
                        "monthname":"มิถุนายน",
                        "weekday_short":"Mon",
                        "weekday":"วันจันทร์",
                        "ampm":"PM",
                        "tz_short":"ICT",
                        "tz_long":"Asia/Bangkok"
                    },
                    "period":4,
                    "high": 
                    {
                        "fahrenheit":"95",
                        "celsius":"35"
                    },
                    "low": 
                    {
                        "fahrenheit":"79",
                        "celsius":"26"
                    },
                    "conditions":"มีโอกาสเกิดพายุฝนฟ้าคะนอง",
                    "icon":"chancetstorms",
                    "icon_url":"http://icons-ak.wxug.com/i/c/k/chancetstorms.gif",
                    "skyicon":"mostlycloudy",
                    "pop":30,
                    "qpf_allday": 
                    {
                        "in": 0.09,
                        "mm": 2.3
                    },
                    "qpf_day": 
                    {
                        "in": 0.06,
                        "mm": 1.5
                    },
                    "qpf_night": 
                    {
                        "in": 0.03,
                        "mm": 0.8
                    },
                    "snow_allday": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "snow_day": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "snow_night": 
                    {
                        "in": 0,
                        "cm": 0
                    },
                    "maxwind":
                    {
                        "mph": 12,
                        "kph": 19,
                        "dir": "ทางทิศตะวันตกเฉียงใต้",
                        "degrees": 224
                    },
                    "avewind": 
                    {
                        "mph": 10,
                        "kph": 16,
                        "dir": "ทางทิศตะวันตกเฉียงใต้",
                        "degrees": 221
                    },
                    "avehumidity": 65,
                    "maxhumidity": 80,
                    "minhumidity": 52
                }
            ]
        }
    }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น