Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active July 11, 2019 02:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save komasaru/5820771 to your computer and use it in GitHub Desktop.
Save komasaru/5820771 to your computer and use it in GitHub Desktop.
Ruby script to get a address or latitude, longitude with Google Geocoding API.
#*********************************************
# 住所から緯度・経度を取得する、又は、
# 緯度・経度から住所を取得する。
# ( by Google Geocode API )
#*********************************************
#
require 'json'
require 'net/http'
BASE_URL = "http://maps.googleapis.com/maps/api/geocode/json"
OPEN_URL = "http://maps.google.co.jp/maps"
USAGE = <<"EOS"
[USAGE] - 住所から緯度・経度を取得する場合
第1引数:住所
- 緯度・経度から住所を取得する場合
第1引数:緯度 ( -90 〜 90)
第2引数:経度 (-180 〜 180)
EOS
class GoogleGeocode
def initialize
@addr = ""
@lat = 0
@lng = 0
end
# コマンドライン引数チェック
def check_args
begin
ret = true
# 引数の個数別にチェック
case ARGV.size
when 0
ret = false
when 1
# 住所(半角数字と半角ピリオドだけなら、明らかに住所ではないのでエラー)
if ARGV[0].to_s =~ /^([-+]?[\d\.]+)$/
ret = false
else
@addr = ARGV[0].to_s
end
when 2
# 緯度(半角数字と半角ピリオド以外、-90〜90以外はエラー)
if ARGV[0].to_s =~ /^([-+]?[\d\.]+)$/
@lat = $1.to_f
ret = false if @lat > 90.0 || @lat < -90.0
else
ret = false
end
# 経度(半角数字と半角ピリオド以外、-180〜180以外はエラー)
if ARGV[1].to_s =~ /^([-+]?[\d\.]+)$/
@lng = $1.to_f
ret = false if @lng > 180.0 || @lng < -180.0
else
ret = false
end
end
return ret
rescue => e
msg = "[EXCEPTION][" + self.class.name + ".check_args] " + e.to_s
STDERR.puts msg
exit 1
end
end
# 主処理
def proc_main
begin
if @addr == ""
# 緯度・経度から住所を取得
url = "#{BASE_URL}?latlng=#{@lat},#{@lng}&sensor=false&language=ja"
res = Net::HTTP.get_response(URI.parse(url))
status = JSON.parse(res.body)
if status['status'] == "OK"
@addr = status['results'][0]['formatted_address']
puts "緯度・経度 : #{@lat}, #{@lng}"
puts "住 所 : #{@addr}"
else
puts "STATUS : #{status['status']}"
end
else
# 住所から緯度・経度を取得
url = "#{BASE_URL}?address=#{URI.encode(@addr)}&sensor=false&language=ja"
res = Net::HTTP.get_response(URI.parse(url))
status = JSON.parse(res.body)
if status['status'] == "OK"
@addr = status['results'][0]['formatted_address']
@lat = status['results'][0]['geometry']['location']['lat']
@lng = status['results'][0]['geometry']['location']['lng']
puts "住 所 : #{@addr}"
puts "緯度・経度 : #{@lat}, #{@lng}"
else
puts "STATUS : #{status['status']}"
end
end
rescue => e
msg = "[EXCEPTION][" + self.class.name + ".proc_main] " + e.to_s
STDERR.puts msg
exit 1
end
end
# ブラウザ表示
# Linux : xdg-open
# MacOS : open
# Windows : start /B
# Cygwin : cmd /C start /B
def open_web
begin
# 緯度・経度をブラウザ表示
system `xdg-open '#{OPEN_URL}?q=#{@addr}&ll=#{@lat},#{@lng}&z=16'`
rescue => e
msg = "[EXCEPTION][" + self.class.name + ".open_web] " + e.to_s
STDERR.puts msg
exit 1
end
end
end
# クラスインスタンス化
obj_main = GoogleGeocode.new
# 引数チェック
unless obj_main.check_args
puts USAGE
exit
end
# 主処理
obj_main.proc_main
# ブラウザ表示
obj_main.open_web
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment