Simple server-side geocoding
Language: Ruby
# Very simple server-side lat/lng lookup via Google's geocoder service
#
# Example:
#
# point = Geocoder.get "Wrigley Field"
# point.lat # => 41.9482
# point.lng # => -87.6557
%w(rubygems rack restclient json).each {|lib| require lib}
module Geocoder
class Location < Struct.new(:name, :lat, :lng); end
def self.get(location)
resp = JSON.parse(get_raw(location))
if placemark = resp["Placemark"]
new_location(location, placemark[0])
end
end
def self.get_raw(location)
uri = "http://maps.google.com/maps/geo?sensor=false&output=json&q=#{Rack::Utils.escape(location)}"
RestClient.get(uri)
end
private
def self.new_location(location, placemark)
Location.new(location, placemark["Point"]["coordinates"][1], placemark["Point"]["coordinates"][0])
end
end
Reveal More

