1 class GeocoderController < ApplicationController
5 require 'rexml/document'
12 if params[:query][:postcode]
13 postcode = params[:query][:postcode]
14 if postcode.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/)
15 #its a zip code - do something
17 Net::HTTP.start('www.freethepostcode.org') do |http|
18 resp = http.get("/geocode?postcode=#{postcode}")
19 lat = resp.body.scan(/[4-6][0-9]\.?[0-9]+/)
20 lon = resp.body.scan(/[-+][0-9]\.?[0-9]+/)
21 @postcode_array = [lat, lon]
22 redirect_to "/index.html?lat=#{@postcode_array[0]}&lon=#{@postcode_array[1]}&zoom=14"
26 if params[:query][:place_name] != nil or ""
27 place_name = params[:query][:place_name]
28 Net::HTTP.start('ws.geonames.org') do |http|
29 res = http.get("/search?q=#{place_name}&maxRows=10")
30 xml = REXML::Document.new(res.body)
31 xml.elements.each("geonames/geoname") do |ele|
33 ele.elements.each("name"){ |n| res_hash['name'] = n.text }
34 ele.elements.each("countryCode"){ |n| res_hash['country_code'] = n.text }
35 ele.elements.each("countryNode"){ |n| res_hash['country_name'] = n.text }
36 ele.elements.each("lat"){ |n| res_hash['lat'] = n.text }
37 ele.elements.each("lng"){ |n| res_hash['lon']= n.text }
42 redirect_to :controller => 'geocoder', :action => 'results', :params => @res_ary
47 @res = :params[@res_ary]