天気API(yahooのようなもの)を使用して、javacriptコードを都市の温度を返すようにしたいと考えていました。 私のアプリはrivescript(javascriptとnodeをベースにしています)で動作します。javascript(serverside)の温度値を取得する方法は?
リサーチ私はjsonやhtmlやcssを使ってローカルで行う方法しか見つけられませんでしたが、ちょうど温度のある値を返す単純なjavascriptコードが必要です。
おかげ
天気API(yahooのようなもの)を使用して、javacriptコードを都市の温度を返すようにしたいと考えていました。 私のアプリはrivescript(javascriptとnodeをベースにしています)で動作します。javascript(serverside)の温度値を取得する方法は?
リサーチ私はjsonやhtmlやcssを使ってローカルで行う方法しか見つけられませんでしたが、ちょうど温度のある値を返す単純なjavascriptコードが必要です。
おかげ
あなたがopenweathermap.org APIを使用して、このような何かを試すことができます。
function getWeather(city, callback) {
var url = 'http://api.openweathermap.org/data/2.5/weather';
$.ajax({
dataType: "jsonp",
url: url,
jsonCallback: 'jsonp',
data: { q: city },
cache: false,
success: function (data) {
callback(data.main.temp);
}
});
}
この例では、入力として都市の名前を使用し、K°度の温度を返します。
data.main.temp
の値が返されますが、data
を返して、その都市の気象オブジェクト全体を取得することができます。それ以外の場合は
、あなたは(あなたのAPPIDで)ヤフー天気APIを使用する場合:
function getWeather(position, callback) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
// Yahoo's PlaceFinder API http://developer.yahoo.com/geo/placefinder/
// We are passing the R gflag for reverse geocoding (coordinates to place name)
var geoAPI = 'http://where.yahooapis.com/geocode?location='+lat+','+lon+'&flags=J&gflags=R&appid='+APPID;
// Forming the query for Yahoo's weather forecasting API with YQL
// http://developer.yahoo.com/weather/
var wsql = 'select * from weather.forecast where woeid=WID and u="'+DEG+'"',
weatherYQL = 'http://query.yahooapis.com/v1/public/yql?q='+encodeURIComponent(wsql)+'&format=json&callback=?', code, city, results, woeid;
// Issue a cross-domain AJAX request (CORS) to the GEO service (not supported in Opera and IE)
$.getJSON(geoAPI, function(r) {
if (r.ResultSet.Found == 1) {
results = r.ResultSet.Results;
city = results[0].city;
code = results[0].statecode || results[0].countrycode;
woeid = results[0].woeid; // the the city identifier for the weather API
// Make a weather API request (it is JSONP, so CORS is not an issue):
$.getJSON(weatherYQL.replace('WID', woeid), function(r) {
if (r.query.count == 1) {
var item = r.query.results.channel.item.condition;
callback(item.temp
} else {
console.error("Error retrieving weather data!");
}
});
}
}).error(function(){
console.error("Sorry, your browser does not support CORS requests!");
});
}
この例では、入力としてposition
を使用しています(navigator.geolocation
を参照)、およびC°度の温度を返します。
注:
- 両方の例は、jQueryのの使用を意味します。
- 2番目の例では、あなたの質問が理解することは本当に難しいですヤフーAPPID
を得た意味します。 APIを書くときは、通常、JSONまたはXMLを返します。クライアント側では、JavaScript(Web)または他の言語(ネイティブ)を使用してデータを要求します。 APIで指定されたJSONを解析する場合は、読み込んで値を読み取ります。 JSONの場合は、JSで特別なアンマーシャリングも必要ありません。 –