百度API 免费接口获取天气预报
发布时间:-08-06 05:38:12
来源:51CTO
阅读:4726
作者:大大果
Document
#result{width: 800px;border:1px solid #ddd;margin-top: 30px;background: #aaa;display: none;}
#result p{height: 25px;line-height: 25px;padding-left: 20px;color: #fff;font-weight: bold;font-size: 16px;}
#result p span{padding: 0 10px;display: inline-block;width: 120px;text-align:center;height: 25px;line-height: 25px;}
#result p span.temp{width: 250px;text-align: left;margin-left: 20px;}
$(function(){
$(window).on('load',function(){
$.ajax({
'type':'get',
'url':'/heweather/weather/free?city=beijing',
'headers':{'apikey':'2c6d2927ef7697018e989f6afbbb'},
'success':function(data){
$('#result').show();
data = data['HeWeather data service 3.0'][0];
var basic_info = '城市:' + ty + '' + data.basic.city;
var tianqi = data.daily_forecast;
$('
' + basic_info + '
').appendTo('#result');
$('
日期气温天气
').appendTo('#result');
$.each(tianqi,function(index,element){
var html = '';
var date = element.date;
var temperature = element.tmp.min + '°C - ' + element.tmp.max + '°C';
var w1 = element.cond.txt_d;
var w2 = element.cond.txt_n;
var weather;
if(w1 == w2){
weather = w1;
}
else{
weather = w1 + ' 转 ' + w2;
}
html += ''+ date +'' + ''+ temperature +'' + ''+ weather +'';
$('
'+ html +'
').appendTo('#result');
});
}
});
});
$('#query').on('click',function(){
$('#result').hide().empty();
var city = $.trim($('#city').val());
$.ajax({
'type':'get',
'url':'/heweather/weather/free',
'data':{'city':city},
'headers':{'apikey':'2c6d2927ef7697018e989f6afbbb'},
'success':function(data){
$('#city').val('');
data = data['HeWeather data service 3.0'][0];
console.log(data);
var basic_info = '城市:' + ty + '' + data.basic.city;
var tianqi = data.daily_forecast;
$('
' + basic_info + '
').appendTo('#result');
$('
日期气温天气
').appendTo('#result');
$.each(tianqi,function(index,element){
var html = '';
var date = element.date;
var temperature = element.tmp.min + '℃ - ' + element.tmp.max + '℃';
var w1 = element.cond.txt_d;
var w2 = element.cond.txt_n;
var weather;
if(w1 == w2){
weather = w1;
}
else{
weather = w1 + ' 转 ' + w2;
}
html += ''+ date +'' + ''+ temperature +'' + ''+ weather +'';
$('
'+ html +'
').appendTo('#result');
$('#result').show();
});
}
});
});
});