- else
- # Some other postcode / zip file
- redirect_to "/index.html?error=unknown_postcode_or_zip"
- end
- else
- if params[:query][:place_name] != nil or ""
- place_name = params[:query][:place_name]
+ elsif postcode.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})
+ /)
+ #its a UK postcode
+ begin
+ 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]+/)
+ redirect_to :controller => params[:next_controller], :action => params[:next_action], :mlat => lat, :mlon => lon, :zoom => 14
+ return
+ end
+ rescue
+ redirect_to :controller => params[:next_controller], :action => params[:next_action], :error => "invalid_postcode"
+ #redirect to somewhere else
+ end
+ elsif
+ # Some other postcode / zip code
+ # Throw it at geonames, and see if they have any luck with it