X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/ff2b0f81c4145171417962163894cee624ff9f73..bf2e2c714066fe9064708552f2e31e64fa0982d3:/app/controllers/geocoder_controller.rb diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index d25a5fa65..7d434f19a 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -1,36 +1,49 @@ class GeocoderController < ApplicationController + layout 'site' require 'net/http' require 'rexml/document' def search - @res_hash = {} + res_hash = {} @postcode_arr = [] - -=begin if params[:geocode][:place] - postcode = params[:geocode][:place] - Net::HTTP.start('ws.geonames.org') do |http| - res = http.get("/search?q=#{query}&maxRows=10") - xml = REXML::Document.new(res.body) - xml.elements.each("/geonames/geoname") do |ele| - ele.elements.each("name"){ |n| @res_hash['name'] = n.text } - ele.elements.each("countryCode"){ |n| @res_hash['country'] = n.text } - ele.elements.each("lat"){ |n| @res_hash['lat'] = n.text } - ele.elements.each("lng"){ |n| @res_hash['lon']= n.text } + @res_ary = [] + + if params[:query][:postcode] + postcode = params[:query][:postcode] + if postcode.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/) + #its a zip code - do something + else + Net::HTTP.start('www.freethepostcode.org') do |http| + resp = http.get("/geocode?postcode=#{postcode}") + lat = resp.body.scan(/[4-6][0-9]\.?[0-9]+/) + lon = resp.body.scan(/[-+][0-9]\.?[0-9]+/) + @postcode_array = [lat, lon] + redirect_to "/index.html?lat=#{@postcode_array[0]}&lon=#{@postcode_array[1]}&zoom=14" + end + end + else + if params[:query][:place_name] != nil or "" + place_name = params[:query][:place_name] + Net::HTTP.start('ws.geonames.org') do |http| + res = http.get("/search?q=#{place_name}&maxRows=10") + xml = REXML::Document.new(res.body) + xml.elements.each("geonames/geoname") do |ele| + res_hash = {} + ele.elements.each("name"){ |n| res_hash['name'] = n.text } + ele.elements.each("countryCode"){ |n| res_hash['country_code'] = n.text } + ele.elements.each("countryNode"){ |n| res_hash['country_name'] = n.text } + ele.elements.each("lat"){ |n| res_hash['lat'] = n.text } + ele.elements.each("lng"){ |n| res_hash['lon']= n.text } + @res_ary << res_hash + end end end + redirect_to :controller => 'geocoder', :action => 'results', :params => @res_ary end -=end + end - if params[:query][:postcode] - postcode = params[:query][:postcode] - Net::HTTP.start('www.freethepostcode.org') do |http| - resp = http.get("/geocode?postcode=#{postcode}") - lat = resp.body.scan(/[4-6][0-9]\.?[0-9]+/) - lon = resp.body.scan(/[-+][0-9]\.?[0-9]+/) - @postcode_array = [lat, lon] - end - redirect_to "/index.html?lat=#{@postcode_array[0]}&lon=#{@postcode_array[1]}&zoom=14" - end + def result + @res = :params[@res_ary] end end