Revision: 11170
Updated Code
at January 27, 2009 03:07 by dbug13
Updated Code
require 'net/http' require 'uri' require "rexml/document" # Retrieves US Location data based on a passed in Zipcode. Uses # Google Maps geocoding service to retrieve a Hash of city, state, and zip # For more info on the service see: http://code.google.com/apis/maps/documentation/geocoding/ # # example: # puts get_location_data(97030).inspect # outputs: # {:state=>"OR", :zip=>"97030", :city=>"Gresham"} def get_location_data(zip) url = "http://maps.google.com/maps/geo" uri = URI.parse(url) req = Net::HTTP::Get.new(uri.path + "?output=xml&q=#{zip}") res = Net::HTTP.start(uri.host, uri.port) do |http| http.request(req) end data = res.body result = {} address = "" doc = REXML::Document.new data doc.elements.each('//Placemark[1]/address') do |element| address = element.text end if address parts = address.split(/[,\s*]/) result[:city] = parts[0] result[:state] = parts[2] result[:zip] = parts[3] end result end
Revision: 11169
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 27, 2009 02:59 by dbug13
Initial Code
require 'net/http' require 'uri' require "rexml/document" # Retrieves US Location data based on a passed in Zipcode. Uses # Google Maps geocoding service to retrieve a Hash of city, state, and zip # # example: # puts get_location_data(97030).inspect # outputs: # {:state=>"OR", :zip=>"97030", :city=>"Gresham"} def get_location_data(zip) url = "http://maps.google.com/maps/geo" uri = URI.parse(url) req = Net::HTTP::Get.new(uri.path + "?output=xml&q=#{zip}") res = Net::HTTP.start(uri.host, uri.port) do |http| http.request(req) end data = res.body result = {} address = "" doc = REXML::Document.new data doc.elements.each('//Placemark[1]/address') do |element| address = element.text end if address parts = address.split(/[,\s*]/) result[:city] = parts[0] result[:state] = parts[2] result[:zip] = parts[3] end result end
Initial URL
Initial Description
Initial Title
Retrieve City, State, and Zip info from Google Geocoding service, by passing in a Zipcode
Initial Tags
google, ruby
Initial Language
Ruby