]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/geocoder_controller.rb
Recognise UK postcodes with a space in the middle.
[rails.git] / app / controllers / geocoder_controller.rb
index 9349eaca4dfc96154a51f05f30d3c2014062e8e9..34a90935e09bfa3f60a99539412e44bfbe8a9883 100644 (file)
@@ -9,7 +9,7 @@ class GeocoderController < ApplicationController
 
     if @query.match(/^\d{5}(-\d{4})?$/)
       @results.push search_us_postcode(@query)
-    elsif @query.match(/(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})/i)
+    elsif @query.match(/(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\s*[0-9][ABD-HJLNP-UW-Z]{2})/i)
       @results.push search_uk_postcode(@query)
     elsif @query.match(/[A-Z]\d[A-Z]\s*\d[A-Z]\d/i)
       @results.push search_ca_postcode(@query)
@@ -91,11 +91,16 @@ private
       place = named.elements["place/named"] || named.elements["nearestplaces/named"]
       type = named.attributes["info"].to_s.capitalize
       name = named.attributes["name"].to_s
-      distance = format_distance(place.attributes["approxdistance"].to_i)
-      direction = format_direction(place.attributes["direction"].to_i)
-      placename = place.attributes["name"].to_s
-      results.push({:lat => lat, :lon => lon, :zoom => zoom,
-                    :description => "#{type} #{name}, #{distance} #{direction} of #{placename}"})
+      if place
+        distance = format_distance(place.attributes["approxdistance"].to_i)
+        direction = format_direction(place.attributes["direction"].to_i)
+        placename = place.attributes["name"].to_s
+        results.push({:lat => lat, :lon => lon, :zoom => zoom,
+                      :description => "#{type} #{name}, #{distance} #{direction} of #{placename}"})
+      else
+        results.push({:lat => lat, :lon => lon, :zoom => zoom,
+                      :description => "#{type} #{name}"})
+      end
     end
 
     return { :source => "OpenStreetMap Namefinder", :url => "http://www.frankieandshadow.com/osm/", :results => results }