这就是我正在使用的:
$.YQL = function(query, callback) {
var encodedQuery = encodeURIComponent(query.toLowerCase()),
url = 'http://query.yahooapis.com/v1/public/yql?q=' + encodedQuery + '&format=json&callback=?';
$.getJSON(url, callback);
};
$.YQL("SELECT * FROM weather.forecast WHERE location = 93065", function(data){
var w = data.query.results.channel;
var d = new Date();
var curr_hour = d.getHours();
var dn = (curr_hour < 18 && curr_hour > 4 ? 'd' : 'n');
var weatherImage = "http://l.yimg.com/a/i/us/nws/weather/gr/" + w.item.condition.code + dn + ".png";
var weatherIcon = "http://l.yimg.com/a/i/us/nws/weather/gr/" + w.item.condition.code + "s.png";
$('.weatherImage').html("<img src='" + weatherImage + "' />");
$('.weatherTemp').html(w.item.condition.temp + "°");
$('.weatherText').html(w.item.condition.text);
$(this).everyTime('300s', function(){
var w = data.query.results.channel;
var d = new Date();
var curr_hour = d.getHours();
var dn = (curr_hour < 18 && curr_hour > 4 ? 'd' : 'n');
var weatherImage = "http://l.yimg.com/a/i/us/nws/weather/gr/" + w.item.condition.code + dn + ".png";
var weatherIcon = "http://l.yimg.com/a/i/us/nws/weather/gr/" + w.item.condition.code + "s.png";
$('.weatherImage').html("<img src='" + weatherImage + "' />");
$('.weatherTemp').html(w.item.condition.temp + "°");
$('.weatherText').html(w.item.condition.text);
});
}).appendTo('.pin-up-weather');
不漂亮,但它是一个短期解决方案。有没有人有定制的天气预报的经验,非常动态?
我希望根据用户浏览的位置更新天气,但我不可能。我看了Googles geolocation API,但是已经不再使用了map api接管的东西,但是我仍然无法解决这个问题。
如果不是我可以让脚本从数据库中获取位置,具体取决于用户在其配置文件中指定的位置。
感谢