flutter 开放天气Onecall API

zynd9foi  于 2022-12-14  发布在  Flutter
关注(0)|答案(3)|浏览(210)

我想使用Onecall API根据用户位置、当前天气和本周剩余时间的天气来获取城市名称,但是我在Onecall API中找不到城市名称,只有一个时区,没有用。是否有其他方法来获取城市名称??以下是JSON API响应:

{
  "lat": 33.44,
  "lon": -94.04,
  "timezone": "America/Chicago",
  "timezone_offset": -18000,
  "current": {
    "dt": 1633679614,
    "sunrise": 1633695331,
    "sunset": 1633737084,
    "temp": 292.71,
    "feels_like": 292.6,
    "pressure": 1017,
    "humidity": 72,
    "dew_point": 287.53,
    "uvi": 0,
    "clouds": 0,
    "visibility": 10000,
    "wind_speed": 1.34,
    "wind_deg": 180,
    "wind_gust": 2.24,
    "weather": [
      {
        "id": 800,
        "main": "Clear",
        "description": "clear sky",
        "icon": "01n"
      }
    ]
  },
ncecgwcz

ncecgwcz1#

此信息不存在。请使用地理编码获取您的城市。

ymdaylpp

ymdaylpp3#

您可以使用这个库-weather_pack。目前有两个可用的API:

  • api.openweathermap.org/data/2.5/weather
  • api.openweathermap.org/data/2.5/onecall

内置地理编码:

const api = 'YOUR_APIKEY';
final String cityName = 'suggested location name';
final String apiKey = 'your api key for OWM';

// use geocoding:
final GeocodingService gService = await GeocodingService(apiKey);
final List<PlaceGeocode> places = gService.getLocationByCityName(cityName);

// use weather service:
final wService = WeatherService(api);
final PlaceGeocode place = places[0];

// get the current weather with use PlaceGeocode
final WeatherOneCall onecall = await 
    wService.oneCallWeatherByLocation(latitude: place.latitude, longitude: place.longitude);

相关问题