【发布时间】:2017-05-22 00:58:16
【问题描述】:
有人知道一个好的网络吗?我能找到的唯一适用于网络的小部件是 accuweather,但他们的邮政编码经常会生成错误的城市,所以它不准确。
我不介意,硬编码,但我不知道每个说的代码。或工作示例。通过邮政编码自动显示用户天气的某些东西
任何建议将不胜感激,谢谢。
【问题讨论】:
有人知道一个好的网络吗?我能找到的唯一适用于网络的小部件是 accuweather,但他们的邮政编码经常会生成错误的城市,所以它不准确。
我不介意,硬编码,但我不知道每个说的代码。或工作示例。通过邮政编码自动显示用户天气的某些东西
任何建议将不胜感激,谢谢。
【问题讨论】:
2 我能想到的替代方案,Yahoo YQL 和 Wunderground API。这 2 个不是基于小部件的,而是用于返回原始天气信息的 API(例如,以 json 格式)。您必须自己进行格式化。
Yahoo YQL 是免费的,但老实说,我不知道您每天可以进行多少次查询的限制。 Yahoo YQL 将要求您使用 woeid 进行查询。例如,
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20u=%27c%27%20and%20woeid=56069&format=json
56069 的 woied 指向阿鲁巴岛的奥拉涅斯塔德。该列表可以从this weather station file 检索。但是,您还需要从 IP2Location 购买商业包以获取匹配的气象站代码。
第二个选项是 Wunderground。有一个开发者版本的 API,但每天限制为 500 次调用。他们的 API 只需要查询的国家代码和城市名称。例如,
http://api.wunderground.com/api/Your_Key/conditions/q/TH/Bangkok.json
其中 TH 是 ISO3166 国家代码,Bangkok 是城市。对于此选项,您可以使用免费的位置网络服务,例如 IPInfoDB 或数据库版本,例如 IP2Location LITE DB,或任何位置服务提供商。
【讨论】:
您可以通过 IP 地址获取城市名称。试试https://geoip-db.com的服务
一个 jQuery 示例(虽然其他代码 sn-ps 也可以在他们的网页上找到):
<!DOCTYPE html>
<html>
<head>
<title>GEOIP DB - City name by IP address</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
</head>
<body>
<div>Country: <span id="country"></span>
<div>State: <span id="state"></span>
<div>City: <span id="city"></span>
<div>Latitude: <span id="latitude"></span>
<div>Longitude: <span id="longitude"></span>
<div>IP: <span id="ip"></span>
<script>
$.ajax({
url: "https://geoip-db.com/jsonp",
jsonpCallback: "callback",
dataType: "jsonp",
success: function( location ) {
$('#country').html(location.country_name);
$('#state').html(location.state);
$('#city').html(location.city);
$('#latitude').html(location.latitude);
$('#longitude').html(location.longitude);
$('#ip').html(location.IPv4);
}
});
</script>
</body>
</html>
【讨论】: