geocoder controller - handles geocoding
[rails.git] / app / controllers / geocoder_controller.rb
1 class GeocoderController < ApplicationController
2
3   require 'net/http'
4   require 'rexml/document'
5
6   def search
7     @res_hash = {}
8     @postcode_arr = []
9     
10 =begin    if params[:geocode][:place]
11       postcode = params[:geocode][:place]
12       Net::HTTP.start('ws.geonames.org') do |http|
13         res = http.get("/search?q=#{query}&maxRows=10")
14         xml = REXML::Document.new(res.body)
15         xml.elements.each("/geonames/geoname") do |ele|
16           ele.elements.each("name"){ |n| @res_hash['name'] = n.text }
17           ele.elements.each("countryCode"){ |n| @res_hash['country'] = n.text }
18           ele.elements.each("lat"){ |n| @res_hash['lat'] = n.text }
19           ele.elements.each("lng"){ |n| @res_hash['lon']= n.text }
20         end
21       end
22     end
23 =end
24
25         if params[:query][:postcode]
26       postcode = params[:query][:postcode]
27       Net::HTTP.start('www.freethepostcode.org') do |http|
28         resp = http.get("/geocode?postcode=#{postcode}")
29         lat = resp.body.scan(/[4-6][0-9]\.?[0-9]+/)
30         lon = resp.body.scan(/[-+][0-9]\.?[0-9]+/)
31         @postcode_array = [lat, lon]
32       end
33       redirect_to "/index.html?lat=#{@postcode_array[0]}&lon=#{@postcode_array[1]}&zoom=14"
34           end
35   end
36 end