2013-08-15 20:40:22 +00:00
|
|
|
class GeoLocation::Gmaps
|
|
|
|
|
2012-04-29 20:47:35 +00:00
|
|
|
def self.geocode(address)
|
|
|
|
url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI::escape address}&sensor=true"
|
|
|
|
response = Net::HTTP.get_response( URI.parse(url) )
|
2013-08-15 20:55:06 +00:00
|
|
|
return if ! response.kind_of? Net::HTTPSuccess
|
2012-04-29 20:47:35 +00:00
|
|
|
|
|
|
|
result = JSON.parse( response.body )
|
|
|
|
|
|
|
|
lat = result['results'].first['geometry']['location']['lat']
|
|
|
|
lng = result['results'].first['geometry']['location']['lng']
|
|
|
|
latlng = [lat,lng]
|
|
|
|
end
|
2013-08-15 20:40:22 +00:00
|
|
|
|
2012-04-29 20:47:35 +00:00
|
|
|
def self.reverse_geocode(lat,lng)
|
|
|
|
url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=#{lat},#{lng}&sensor=true"
|
|
|
|
response = Net::HTTP.get_response( URI.parse(url) )
|
2013-08-15 20:55:06 +00:00
|
|
|
return if ! response.kind_of? Net::HTTPSuccess
|
2012-04-29 20:47:35 +00:00
|
|
|
|
|
|
|
result = JSON.parse( response.body )
|
2013-08-15 20:40:22 +00:00
|
|
|
|
2012-04-29 20:47:35 +00:00
|
|
|
address = result['results'].first['address_components'].first['long_name']
|
|
|
|
return address
|
|
|
|
end
|
2013-08-15 20:40:22 +00:00
|
|
|
end
|