问题描述
我正在尝试使用openweather API来查找7天天气预报,但是当我调用openweather API时,当前位置的数据不来了,有人可以指导我找到正确的API来获取天气预报当前位置。
到目前为止已使用的API
1. api.openweathermap.org/data/2.5/forecast?q={city name}&appid={API key}
2. https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude={part}&appid={API key}
参考链接:
请求:https://api.openweathermap.org/data/2.5/forecast?q=India&appid={MY_API_ID}
回复:(仅在40天中添加了2天的数据)
{
"cod": "200","message": 0,"cnt": 40,"list": [
{
"dt": 1600668000,"main": {
"temp": 283.42,"feels_like": 282.81,"temp_min": 283.42,"temp_max": 284.26,"pressure": 1018,"sea_level": 1018,"grnd_level": 862,"humidity": 84,"temp_kf": -0.84
},"weather": [
{
"id": 802,"main": "Clouds","description": "scattered clouds","icon": "03d"
}
],"clouds": {
"all": 32
},"wind": {
"speed": 0.1,"deg": 22
},"visibility": 10000,"pop": 0,"sys": {
"pod": "d"
},"dt_txt": "2020-09-21 06:00:00"
},{
"dt": 1600722000,"main": {
"temp": 283.34,"feels_like": 282.29,"temp_min": 283.34,"temp_max": 283.34,"pressure": 1016,"sea_level": 1016,"grnd_level": 861,"humidity": 86,"temp_kf": 0
},"weather": [
{
"id": 500,"main": "Rain","description": "light rain","icon": "10n"
}
],"clouds": {
"all": 66
},"wind": {
"speed": 0.82,"deg": 149
},"visibility": 823,"pop": 0.35,"rain": {
"3h": 0.18
},"sys": {
"pod": "n"
},"dt_txt": "2020-09-21 21:00:00"
}
],"city": {
"id": 3168508,"name": "Innichen","coord": {
"lat": 46.7406,"lon": 12.2797
},"country": "IT","population": 3107,"timezone": 7200,"sunrise": 1600664205,"sunset": 1600708262
}
}
解决方法
您遇到了很多问题,如果可以缩小范围,这将很有帮助。但是,这是我尽力提供的最佳方法:
对于7天的预测,您需要7天的预测。。
在示例1. api.openweathermap.org/data/2.5/forecast?q={city name}&appid={API key}
中,您还包含了link that you used ,该请求用于5天的请求。如果要7天,则是个问题。
如果要进行5天预报,则在第一个示例中引用的API 需要城市名称,(如果要包括国家/地区,则)要求将国家名称设置为符合ISO 3166 ,因此印度将是“ IND”,而不是印度。
您想要5天还是7天,都需要比印度更具体。
在5天的情况下,您需要输入城市名称。对于孟买的5天预报,您可以使用在第一个示例中尝试过的API,如下所示:
"https://api.openweathermap.org/data/2.5/forecast?q=mumbai&appid={yourAPIKey}"
如果是7天,则需要经纬度坐标。
对于孟买的7天天气预报,您可以使用在第二个示例中尝试过的API,如下所示:
//includes lat and long for mumbai
"https://api.openweathermap.org/data/2.5/onecall?lat=19.0760&lon=72.8777&appid={yourAPIkey}"
我希望这能回答您的问题。