次のプログラムを、Node.js で記述してみました。
緯度経度から都市の名前を得る
reverse_geo.js
# ! /usr/bin/node
//
// reverse_geo.js
//
// May/04/2018
//
// ------------------------------------------------------------------
function parser_results(dict_aa)
{
var unit_out = {}
unit_out['city'] = ""
unit_out['pref'] = ""
const components = dict_aa['results'][0]['address_components']
for (var it in components)
{
const unit_aa = components[it]
if (unit_aa['types'][0] == "locality")
{
unit_out.city = unit_aa['short_name']
}
else if (unit_aa['types'][0] == "administrative_area_level_1")
{
unit_out.pref = unit_aa['short_name']
}
}
return unit_out
}
// ------------------------------------------------------------------
function get_city_pref_proc(lat,lon)
{
api_key="***************************************"
url_aa="https://maps.googleapis.com/maps/api/geocode/json?latlng="
url=url_aa + lat + "," + lon + "&language=ja&key=" + api_key
// console.error(url)
var Client = require('node-rest-client').Client
var client = new Client()
client.get(url, function (data,response)
{
const unit_aa = parser_results(data)
console.log(lat,lon,unit_aa.city,unit_aa.pref)
})
}
// ------------------------------------------------------------------
console.error("*** 開始 ***")
//
//
get_city_pref_proc(43.08,141.35)
get_city_pref_proc(36.56,139.88)
get_city_pref_proc(35.91,139.66)
get_city_pref_proc(35.68,139.76)
get_city_pref_proc(35.16,136.90)
get_city_pref_proc(34.70,135.50)
get_city_pref_proc(33.59,130.42)
get_city_pref_proc(31.56,130.56)
get_city_pref_proc(26.21,127.68)
//
console.error("*** 終了 ***")
// ------------------------------------------------------------------
実行結果
$ ./reverse_geo.js
*** 開始 ***
*** 終了 ***
33.59 130.42 '福岡市' '福岡県'
34.7 135.5 '大阪市' '大阪府'
35.16 136.9 '名古屋市' '愛知県'
35.91 139.66 'さいたま市' '埼玉県'
31.56 130.56 '鹿児島市' '鹿児島県'
36.56 139.88 '宇都宮市' '栃木県'
43.08 141.35 '札幌市' '北海道'
26.21 127.68 '那覇市' '沖縄県'
35.68 139.76 '千代田区' '東京都'