LoginSignup
70
72

More than 5 years have passed since last update.

OpenWeatherMap API v2.5から天気を取得する

Last updated at Posted at 2013-12-06

OpenWeatherMap APIにアクセスしてみたのでメモ。今のところ安定して動いているし無料サービスにしては内容も精度も必要十分かなと思います。天気予報取れるし。

AFNetworking使ったサンプルですみません。

// OpenWeatherMapの天気出力APIエンドポイント
NSString *kOpenWeatherMap3HoursForecastAPI @"http://api.openweathermap.org/data/2.5/weather?units=metric&lat=%f&lon=%f";

// 適当な緯度経度
CLLocationDegrees latitude = 39.01;
CLLocationDegrees longitude = 141.68;

NSString *urlString = [NSString stringWithFormat:kOpenWeatherMap3HoursForecastAPI, latitude, longitude];

[[AFHTTPRequestOperationManager manager] GET:urlString
                                  parameters:nil
                                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
}   
                                     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

OpenWeatherMapで定義された JSON形式のWeatherオブジェクトが取得できます。

{
    base = "cmc stations";
    clouds =     {
        all = 8;
    };
    cod = 200;
    coord =     {
        lat = "39.01";
        lon = "141.68";
    };
    dt = 1386343001;
    id = 2111530;
    main =     {
        "grnd_level" = "1003.63";
        humidity = 100;
        pressure = "1003.63";
        "sea_level" = "1024.69";
        temp = "-1.631";
        "temp_max" = "-1.631";
        "temp_min" = "-1.631";
    };
    name = Ofunato;
    sys =     {
        country = JP;
        message = "0.0047";
        sunrise = 1386279482;
        sunset = 1386313849;
    };
    weather =     (
                {
            description = "sky is clear";
            icon = 02n;
            id = 800;
            main = Clear;
        }
    );
    wind =     {
        deg = "281.503";
        speed = "2.31";
    };
}

Weatherオブジェクトデータ形式

キー 意味 出力するかどうか
id 街のID 常に出力
dt データの時刻。GMTのunixtime形式(いわゆるEPOCH) 常に出力
coord.lat coord.lng 街の緯度経度 常に出力
name 街名 常に出力
main.temp 気温(ケルビン)。摂氏に変換するには273.15を引く。 常に出力
main.humidity 湿度(%) 常に出力
main.temp_min main.temp_max 最低気温、最高気温 オプション
main.pressure 気圧(hPa) 常に出力
wind.speed 風速(meter / sec) 常に出力
wind.deg 風向き (度。meteorological 時計回り?) 常に出力
clouds.all 雲の割合?(Cloudiness in %) 常に出力
weather 気象状態 オプション
rain.3h 3時間毎の降水量 オプション
snow.3h 3時間ごとの降雪量 オプション

weather.description(天気の概要)、name(土地名)は多言語化されているようなのですが、残念ながら日本語はサポートされていません。

単位について

imperial, metricを選択することが出来ます。先の例ではunits=metricを指定して摂氏で取得しています。

項目 default imperial metric
温度 ケルビン 華氏 摂氏
気圧 hPa hPa hPa
風速 メートル / 秒 不明 メートル / 秒

単位指定が無い場合は気温がケルビンになる点に注意。

単位にimperialを指定した際の風速はノットでもないしmile / secでもないしバグってるかも?(Bug #111 Wrong wind speed value with imperial units

参考

70
72
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
70
72