Skip to content

Instantly share code, notes, and snippets.

@sankichi92
Last active July 9, 2016 06:55
Show Gist options
  • Select an option

  • Save sankichi92/4841aedce0cd8a1922a7f4f2009e53d8 to your computer and use it in GitHub Desktop.

Select an option

Save sankichi92/4841aedce0cd8a1922a7f4f2009e53d8 to your computer and use it in GitHub Desktop.
Service Computing Assignment 4 - Practice of Information Systems
require 'net/http'
require 'uri'
require 'json'
raise ArgumentError, "required argument 'zipcode' not found" if ARGV.empty?
ZIPCODE = ARGV[0]
API_KEY = ''.freeze # TODO: fill in your Google Maps Geocoding API key
def get_hash_from_json_web_api(uri, **params)
uri.query = URI.encode_www_form(params)
# puts uri
res = Net::HTTP.get_response(uri)
# puts res.body
JSON.parse(res.body)
end
uri1 = URI('http://zipcloud.ibsnet.co.jp/api/search')
hash1 = get_hash_from_json_web_api(uri1, zipcode: ZIPCODE)
result = hash1['results'][0]
address = result['address1'] + result['address2'] + result['address3']
puts "address: #{address}"
uri2 = URI('https://maps.googleapis.com/maps/api/geocode/json')
hash2 = get_hash_from_json_web_api(uri2, address: address, key: API_KEY)
location = hash2['results'][0]['geometry']['location']
puts "latitude: #{location['lat']}"
puts "longitude: #{location['lng']}"
@sankichi92
Copy link
Author

sankichi92 commented Jul 7, 2016

Usage

Give a zipcode as an argument.

$ ruby zipcode_to_geocode.rb 6068246

Then you can get following results.

address: 京都府京都市左京区北白川西平井町
latitude: 35.0352314
longitude: 135.7860501

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment