【发布时间】:2016-12-08 15:31:11
【问题描述】:
我刚刚用OpenWeatherMap创建了一个帐户
我想通过 City ID API 调用获取当前位置的天气:
http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=myAPIKey
在我的 html 页面上,我该如何使用它,以便向我的用户展示特定位置的天气情况?
我使用过 Yahoo Weather API,但想尝试一些不同的东西。过程类似吗?我看不出我会在哪里调用像在 yahoo 天气 api 中那样的回调函数。
我必须写这样的东西吗?
<script src="http://api.openweathermap.org/data/2.5/weather?id=2172797&mode=html&appid=myapikey"></script>
我已经尝试过了,但它不起作用..我在网站上找不到任何关于如何将其集成到我的 html 页面中的示例。
感谢任何帮助。
更新:
我试过了:
<img id="Weather-Image" src="http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=myapikey" />
这会上传一个黑色的 x.. 不是当前天气的图片。
更新:
我发现我需要使用 ajax.. 这是我目前所拥有的:
<script>
$(document).ready(function () {
var request = $.ajax({
url: "http://api.openweathermap.org/data/2.5/weather",
method: "GET",
data: { id: '2172797', appid: 'myapikey' },
success: function (response) {
var dataArray = JSON.parse(response);
var weatherIcon = dataArray.weather.icon;
var iconUrl = "http://openweathermap.org/img/w/" + weatherIcon + ".png";
document.getElementById('Weather-Image').src = iconUrl;
}
});
});
</script>
【问题讨论】:
标签: javascript html openweathermap