Ruby + Yahoo!ジオコーダAPI で住所文字列から緯度経度や住所情報を取得する

Ruby と YOLP Yahoo!ジオコーダAPI で住所文字列から緯度経度や住所情報を取得するサンプルコード。

## ソースコード

```ruby
require 'json'
require 'open-uri'

def geocoder(query, yjdn_app_id)

  # YOLP(地図):Yahoo!ジオコーダAPI - Yahoo!デベロッパーネットワーク
  # https://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/geocoder.html
  base_url = 'https://map.yahooapis.jp/geocode/V1/geoCoder'
  params = {
    'appid' => yjdn_app_id,
    'query' => query,
    'results' => '1',
    'detail' => 'full',
    'output' => 'json',
  }
  url = base_url + '?' + URI.encode_www_form(params)
  puts 'Geocoder URL: ' + url

  json = open(url).read
  data = JSON.parse(json)
  lonlat = data['Feature'][0]['Geometry']['Coordinates']
  puts "経度,緯度: #{lonlat}"

  puts JSON.pretty_generate(data)
end

# Yahoo!デベロッパーネットワークのアプリケーションIDを指定
yjdn_app_id = '<YOUR APPID>'

# 住所文字列
list = [
  '愛知県名古屋市中村区名駅1丁目1番1号',
  '愛知県名古屋市中村区名駅1丁目',
  '名古屋市中村区',
]

list.each do |query|
  puts '-------------------------------------------------'
  puts "検索する住所: #{query}"
  geocoder(query, yjdn_app_id)
end
```

## 実行結果

```
-------------------------------------------------
検索する住所: 愛知県名古屋市中村区名駅1丁目1番1号
Geocoder URL: https://map.yahooapis.jp/geocode/V1/geoCoder?appid=<YOUR APPID>&query=%E6%84%9B%E7%9F%A5%E7%9C%8C%E5%90%8D%E5%8F%A4%E5%B1%8B%E5%B8%82%E4%B8%AD%E6%9D%91%E5%8C%BA%E5%90%8D%E9%A7%851%E4%B8%81%E7%9B%AE1%E7%95%AA1%E5%8F%B7&results=1&detail=full&output=json
経度,緯度: 136.88239465,35.17310142
{
  "ResultInfo": {
    "Count": 1,
    "Total": 1,
    "Start": 1,
    "Status": 200,
    "Description": "",
    "Copyright": "",
    "Latency": 0.141
  },
  "Feature": [
    {
      "Id": "23105.115.1.1.1",
      "Gid": "",
      "Name": "愛知県名古屋市中村区名駅1丁目1-1",
      "Geometry": {
        "Type": "point",
        "Coordinates": "136.88239465,35.17310142",
        "BoundingBox": "136.87937400,35.16753300 136.88560700,35.17408300"
      },
      "Category": [

      ],
      "Description": "",
      "Style": [

      ],
      "Property": {
        "Uid": "d532caf76ac8a57b2a9c7caad9728e0e21fde1c9",
        "CassetteId": "b22fee69b0dcaf2c2fe2d6a27906dafc",
        "Yomi": "アイチケンナゴヤシナカムラクメイエキ1チョウメ",
        "Country": {
          "Code": "JP",
          "Name": "日本"
        },
        "Address": "愛知県名古屋市中村区名駅1丁目1-1",
        "AddressElement": [
          {
            "Name": "愛知県",
            "Kana": "あいちけん",
            "Level": "prefecture"
          },
          {
            "Name": "名古屋市中村区",
            "Kana": "なごやしなかむらく",
            "Level": "city"
          },
          {
            "Name": "名駅",
            "Kana": "めいえき",
            "Level": "oaza"
          },
          {
            "Name": "1丁目",
            "Kana": "1ちょうめ",
            "Level": "aza"
          },
          {
            "Name": "1",
            "Kana": "1",
            "Level": "detail1"
          },
          {
            "Name": "1",
            "Kana": "1",
            "Level": "detail2"
          }
        ],
        "GovernmentCode": "23105",
        "AddressMatchingLevel": "6",
        "Approximation": 1,
        "AddressType": "地番・戸番",
        "OpenForBusiness": "",
        "Detail": {
          "NameHiragana": "あいちけんなごやしなかむらくめいえき1ちょうめ",
          "OldAddressFlag": "false"
        }
      }
    }
  ]
}
-------------------------------------------------
検索する住所: 愛知県名古屋市中村区名駅1丁目
Geocoder URL: https://map.yahooapis.jp/geocode/V1/geoCoder?appid=<YOUR APPID>&query=%E6%84%9B%E7%9F%A5%E7%9C%8C%E5%90%8D%E5%8F%A4%E5%B1%8B%E5%B8%82%E4%B8%AD%E6%9D%91%E5%8C%BA%E5%90%8D%E9%A7%851%E4%B8%81%E7%9B%AE&results=1&detail=full&output=json
経度,緯度: 136.88392512,35.16956864
{
  "ResultInfo": {
    "Count": 1,
    "Total": 15,
    "Start": 1,
    "Status": 200,
    "Description": "",
    "Copyright": "",
    "Latency": 0.046
  },
  "Feature": [
    {
      "Id": "23105.115.1",
      "Gid": "",
      "Name": "愛知県名古屋市中村区名駅1丁目",
      "Geometry": {
        "Type": "point",
        "Coordinates": "136.88392512,35.16956864",
        "BoundingBox": "136.87937400,35.16753300 136.88560700,35.17408300"
      },
      "Category": [

      ],
      "Description": "",
      "Style": [

      ],
      "Property": {
        "Uid": "b94c81d7472bf8ddd35a9bd2ea3409c41edc4f9c",
        "CassetteId": "b22fee69b0dcaf2c2fe2d6a27906dafc",
        "Yomi": "アイチケンナゴヤシナカムラクメイエキ1チョウメ",
        "Country": {
          "Code": "JP",
          "Name": "日本"
        },
        "Address": "愛知県名古屋市中村区名駅1丁目",
        "AddressElement": [
          {
            "Name": "愛知県",
            "Kana": "あいちけん",
            "Level": "prefecture"
          },
          {
            "Name": "名古屋市中村区",
            "Kana": "なごやしなかむらく",
            "Level": "city"
          },
          {
            "Name": "名駅",
            "Kana": "めいえき",
            "Level": "oaza"
          },
          {
            "Name": "1丁目",
            "Kana": "1ちょうめ",
            "Level": "aza"
          }
        ],
        "GovernmentCode": "23105",
        "AddressMatchingLevel": "4",
        "Approximation": 1,
        "AddressType": "丁目・字",
        "OpenForBusiness": "",
        "Detail": {
          "NameHiragana": "あいちけんなごやしなかむらくめいえき1ちょうめ",
          "OldAddressFlag": "false"
        }
      }
    }
  ]
}
-------------------------------------------------
検索する住所: 名古屋市中村区
Geocoder URL: https://map.yahooapis.jp/geocode/V1/geoCoder?appid=<YOUR APPID>&query=%E5%90%8D%E5%8F%A4%E5%B1%8B%E5%B8%82%E4%B8%AD%E6%9D%91%E5%8C%BA&results=1&detail=full&output=json
経度,緯度: 136.87302300,35.16874990
{
  "ResultInfo": {
    "Count": 1,
    "Total": 33435,
    "Start": 1,
    "Status": 200,
    "Description": "",
    "Copyright": "",
    "Latency": 0.017
  },
  "Feature": [
    {
      "Id": "23105",
      "Gid": "",
      "Name": "愛知県名古屋市中村区",
      "Geometry": {
        "Type": "point",
        "Coordinates": "136.87302300,35.16874990",
        "BoundingBox": "136.82910800,35.14506700 136.89302600,35.19166400"
      },
      "Category": [

      ],
      "Description": "",
      "Style": [

      ],
      "Property": {
        "Uid": "f4817a860ab2f5ba730572f6936459cf35568c67",
        "CassetteId": "b22fee69b0dcaf2c2fe2d6a27906dafc",
        "Yomi": "アイチケンナゴヤシナカムラク",
        "Country": {
          "Code": "JP",
          "Name": "日本"
        },
        "Address": "愛知県名古屋市中村区",
        "AddressElement": [
          {
            "Name": "愛知県",
            "Kana": "あいちけん",
            "Level": "prefecture"
          },
          {
            "Name": "名古屋市中村区",
            "Kana": "なごやしなかむらく",
            "Level": "city"
          }
        ],
        "GovernmentCode": "23105",
        "AddressMatchingLevel": "2",
        "Approximation": 1,
        "AddressType": "区",
        "OpenForBusiness": "",
        "Detail": {
          "NameHiragana": "あいちけんなごやしなかむらく",
          "OldAddressFlag": "false"
        }
      }
    }
  ]
}
```

## 参考資料
- [YOLP\(地図\):Yahoo\!ジオコーダAPI \- Yahoo\!デベロッパーネットワーク](https://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/geocoder.html)
- [Ruby \+ Yahoo\!リバースジオコーダAPI で緯度経度から住所情報を取得する \- Qiita](https://qiita.com/niwasawa/items/c8937282d52b0427c1ee)