From d2bd78627e3903e990ddb28aebb39653ebb22fcd Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Fri, 24 Aug 2007 18:53:30 +0000 Subject: [PATCH] More search improvements - a "Where am I?" link that does a reverse search, and various cleanups to the previous changes. --- app/controllers/geocoder_controller.rb | 47 +++++++++++++++++++++++++- app/views/geocoder/_results.rhtml | 3 +- app/views/site/_search.rhtml | 11 ++++++ app/views/site/index.rhtml | 4 +++ config/routes.rb | 1 + public/stylesheets/site.css | 11 ++++-- 6 files changed, 73 insertions(+), 4 deletions(-) diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index f557668b6..6c4afeb90 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -21,15 +21,32 @@ class GeocoderController < ApplicationController results_count = count_results(results) render :update do |page| + page.replace_html :search_results_content, :partial => 'results', :object => results + if results_count == 1 position = results.collect { |s| s[:results] }.compact.flatten[0] page.call "setPosition", position[:lat], position[:lon], position[:zoom] else - page.replace_html :search_results_content, :partial => 'results', :object => results page.call "openSearchResults" end end end + + def description + results = Array.new + + lat = params[:lat] + lon = params[:lon] + + results.push description_osm_namefinder("cities", lat, lon, 2) + results.push description_osm_namefinder("towns", lat, lon, 4) + results.push description_osm_namefinder("places", lat, lon, 10) + + render :update do |page| + page.replace_html :search_results_content, :partial => 'results', :object => results + page.call "openSearchResults" + end + end private @@ -151,6 +168,34 @@ private return { :source => "GeoNames", :url => "http://www.geonames.org/", :error => "Error contacting ws.geonames.org: #{ex.to_s}" } end + def description_osm_namefinder(types, lat, lon, max) + results = Array.new + + # ask OSM namefinder + response = fetch_xml("http://www.frankieandshadow.com/osm/search.xml?find=#{types}+near+#{lat},#{lon}&max=#{max}") + + # parse the response + response.elements.each("searchresults/named") do |named| + lat = named.attributes["lat"].to_s + lon = named.attributes["lon"].to_s + zoom = named.attributes["zoom"].to_s + place = named.elements["place/named"] || named.elements["nearestplaces/named"] + type = named.attributes["info"].to_s + name = named.attributes["name"].to_s + description = named.elements["description"].to_s + distance = format_distance(place.attributes["approxdistance"].to_i) + direction = format_direction(360 - place.attributes["direction"].to_i) + prefix = "#{distance} #{direction} of #{type} " + results.push({:lat => lat, :lon => lon, :zoom => zoom, + :prefix => prefix.capitalize, :name => name, + :description => description}) + end + + return { :type => types.capitalize, :source => "OpenStreetMap Namefinder", :url => "http://www.frankieandshadow.com/osm/", :results => results } + rescue Exception => ex + return { :type => types.capitalize, :source => "OpenStreetMap Namefinder", :url => "http://www.frankieandshadow.com/osm/", :error => "Error contacting www.frankieandshadow.com: #{ex.to_s}" } + end + def fetch_text(url) return Net::HTTP.get(URI.parse(url)) end diff --git a/app/views/geocoder/_results.rhtml b/app/views/geocoder/_results.rhtml index 477f2a0bc..9bcc5d8ae 100644 --- a/app/views/geocoder/_results.rhtml +++ b/app/views/geocoder/_results.rhtml @@ -1,5 +1,6 @@ <% results.each do |source| %> -

Results from <%= link_to source[:source], source[:url] %>

+<% type = source[:type] || "Results" %> +

<%= type %> from <%= link_to source[:source], source[:url] %>

<% if source[:results] %> <% if source[:results].empty? %>

No results found

diff --git a/app/views/site/_search.rhtml b/app/views/site/_search.rhtml index aa9294236..14f8fa5f6 100644 --- a/app/views/site/_search.rhtml +++ b/app/views/site/_search.rhtml @@ -12,6 +12,7 @@