]> git.openstreetmap.org Git - rails.git/blob - app/helpers/geocoder_helper.rb
f5519ddc3fcd5cc0eca4d9d8ebc3638141d7eea3
[rails.git] / app / helpers / geocoder_helper.rb
1 module GeocoderHelper
2   def result_to_html(result)
3     html_options = { :class => "set_position", :data => {} }
4
5     if result[:min_lon] and result[:min_lat] and result[:max_lon] and result[:max_lat]
6       url = "?minlon=#{result[:min_lon]}&minlat=#{result[:min_lat]}&maxlon=#{result[:max_lon]}&maxlat=#{result[:max_lat]}"
7     else
8       url = "?mlat=#{result[:lat]}&mlon=#{result[:lon]}&zoom=#{result[:zoom]}"
9     end
10
11     result.each do |key,value|
12       html_options[:data][key.to_s.tr('_', '-')] = value
13     end
14
15     html = ""
16     html << result[:prefix] if result[:prefix]
17     html << " " if result[:prefix] and result[:name]
18     html << link_to(result[:name], url, html_options) if result[:name]
19     html << result[:suffix] if result[:suffix]
20
21     return raw(html)
22   end
23
24   def describe_location(lat, lon, zoom = nil, language = nil)
25     zoom = zoom || 14
26     language = language || request.user_preferred_languages.join(',')
27     url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
28
29     begin
30       response = OSM::Timer.timeout(4) do
31         REXML::Document.new(Net::HTTP.get(URI.parse(url)))
32       end
33     rescue Exception
34       response = nil
35     end
36
37     if response and result = response.get_text("reversegeocode/result")
38       result.to_s
39     else
40       "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
41     end
42   end
43 end