0

気象アプリケーションに Yahoo API を使用するのに苦労しています。小規模なテストのために、風の寒さだけで気象情報を表示しようとしています。

index.html

<head>
<title>Alex Webber - MyWeather</title>
<script language="javascript" type="text/javascript" src="js/weather">     </script>
</head>
<body>
<p id="demo"><script>weather();</script></p>
</body> 

天気.js

function weather(){
var callbackFunction = function(data) {
var wind = data.query.results.channel.wind;
document.getElementById("demo").innerHTML = wind.chill; 
};
}
4

1 に答える 1

0

yahoo weather api に QUERY を呼び出す必要がありますが、それを省略しているようです。風以外のデータを返すことができるように、クエリは * に設定されています。さらに、すべての地域をカバーするために、変数の場所がクエリに割り当てられます。米国の地域には州名を使用してください。ニューヨークはシティボックス、ニューヨークはカントリーボックス。

<!DOCTYPE html>
<html>
    <head>
        <script>

            function getLocation() {
               var city = document.getElementById("city").value;
               var country = document.getElementById("country").value;
               var location = city + "," + country;

                //create a script and add it to your page
                var script = document.createElement('script');
                script.src = "https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='" + location + "')&format=json&callback=callbackFunction";
                document.getElementsByTagName('head')[0].appendChild(script);

                }


                var callbackFunction = function(data) {
                var wind = data.query.results.channel.wind;
                var wind_chill = wind.chill;
                var wind_speed = wind.speed;
                var wind_direction = wind.direction
                document.getElementById("chill").innerHTML = wind_chill;
                document.getElementById("speed").innerHTML = wind_speed;
                document.getElementById("direction").innerHTML = wind_direction;
                //alert(wind.chill);
            }
        </script>     

    </head>
    <body>
        City:<input type="text" id="city">
        Country/State in U.S.A<input type="text" id="country">
        <input type="button" onclick="getLocation()" value="Get Weather">
        <p>Wind Chill</p>
        <p id="chill"></p>
        <p>Wind Speed</p>
        <p id="speed"></p>
        <p>Wind Direction</p>
        <p id="direction"></p>
    </body>
</html>

寄稿者の Brian Glaz がこれを手伝ってくれて、変数の場所をクエリに割り当てる方法と、データが入力された後にクエリを呼び出す方法を教えてくれました。

于 2015-03-21T08:51:00.037 に答える