Skip to content

Instantly share code, notes, and snippets.

@marcus
Created October 26, 2008 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcus/19962 to your computer and use it in GitHub Desktop.
Save marcus/19962 to your computer and use it in GitHub Desktop.
# By Louis-Philippe Gauthier
# http://lpgauth.tumblr.com/post/56451143/reverse-geocoding-google-ruby
require 'rubygems'
require 'net/http'
require 'xmlsimple'
def reverse_geocode(lat, lng)
url = "http://maps.google.com/maps/geo?ll=#{lat.to_s},#{lng.to_s}&output=xml"
uri = URI.parse(url)
req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
res = Net::HTTP.start(uri.host, uri.port) { |http| http.request(req) }
doc = XmlSimple.xml_in(res.body)
response = doc['Response'].first
if response['Status'].first['code'].first.to_i == 200
placemark = response['Placemark'].first
address = placemark['address']
end
end
puts reverse_geocode(45.50, -73.60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment