Created
November 11, 2016 04:52
-
-
Save shockby/a5cd2f84471489f8b731e39bbc5c078e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'net/http' | |
# usage | |
# GoogleMapGeocoding.new.set_store | |
# class priori | |
class GoogleMapGeocoding | |
GEOCODING_BASE_URL = "https://maps.googleapis.com/maps/api/geocode/json" | |
def initialize | |
@key = "" | |
end | |
def search(address) | |
url = "#{GEOCODING_BASE_URL}?address=#{URI.encode(address)}&key=#{@key}" | |
p url | |
res = Net::HTTP.get_response(URI.parse(url)) | |
status = JSON.parse(res.body) | |
if status['status'] == "OK" | |
address = status['results'][0]['formatted_address'] | |
latitude = status['results'][0]['geometry']['location']['lat'] | |
longitude = status['results'][0]['geometry']['location']['lng'] | |
return [address, latitude, longitude] | |
else | |
return false | |
end | |
end | |
def get_address(data) | |
prefecture = Prefecture.find(data.prefecture_id) | |
"#{prefecture.name}#{data.city_name}#{data.town}" | |
end | |
def set_retail_store | |
Store.all.each do |data| | |
address = get_address(data) | |
ret_address, latitude, longitude = search(address) | |
p address | |
p ret_address, latitude, longitude | |
data.update_attributes(latitude: latitude.to_f, longitude: longitude.to_f) if ret_address | |
sleep 0.1 | |
end | |
return | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment