+ prefix = t "geocoder.search_osm_nominatim.prefix_format", :name => prefix_name
+ object_type = place.attributes["osm_type"]
+ object_id = place.attributes["osm_id"]
+
+ @results.push(:lat => lat, :lon => lon,
+ :min_lat => min_lat, :max_lat => max_lat,
+ :min_lon => min_lon, :max_lon => max_lon,
+ :prefix => prefix, :name => name,
+ :type => object_type, :id => object_id)
+ end
+
+ render :action => "results"
+ rescue StandardError => e
+ @error = "Error contacting nominatim.openstreetmap.org: #{e}"
+ render :action => "error"
+ end
+
+ def search_geonames
+ # get query parameters
+ query = params[:query]
+
+ # get preferred language
+ lang = I18n.locale.to_s.split("-").first
+
+ # create result array
+ @results = []
+
+ # ask geonames.org
+ response = fetch_xml("http://api.geonames.org/search?q=#{escape_query(query)}&lang=#{lang}&maxRows=20&username=#{Settings.geonames_username}")
+
+ # parse the response
+ response.elements.each("geonames/geoname") do |geoname|
+ lat = geoname.text("lat")
+ lon = geoname.text("lng")
+ name = geoname.text("name")
+ country = geoname.text("countryName")
+
+ @results.push(:lat => lat, :lon => lon,
+ :zoom => Settings.geonames_zoom,
+ :name => name,
+ :suffix => ", #{country}")
+ end
+
+ render :action => "results"
+ rescue StandardError => e
+ @error = "Error contacting api.geonames.org: #{e}"
+ render :action => "error"
+ end
+
+ def search_osm_nominatim_reverse
+ # get query parameters
+ lat = params[:lat]
+ lon = params[:lon]
+ zoom = params[:zoom]
+
+ # create result array
+ @results = []
+
+ # ask nominatim
+ response = fetch_xml("#{Settings.nominatim_url}reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}")
+
+ # parse the response
+ response.elements.each("reversegeocode/result") do |result|
+ lat = result.attributes["lat"]
+ lon = result.attributes["lon"]
+ object_type = result.attributes["osm_type"]
+ object_id = result.attributes["osm_id"]
+ description = result.text
+
+ @results.push(:lat => lat, :lon => lon,
+ :zoom => zoom,
+ :name => description,
+ :type => object_type, :id => object_id)
+ end
+
+ render :action => "results"
+ rescue StandardError => e
+ @error = "Error contacting nominatim.openstreetmap.org: #{e}"
+ render :action => "error"
+ end
+
+ def search_geonames_reverse
+ # get query parameters
+ lat = params[:lat]
+ lon = params[:lon]
+
+ # get preferred language
+ lang = I18n.locale.to_s.split("-").first
+
+ # create result array
+ @results = []
+
+ # ask geonames.org
+ response = fetch_xml("http://api.geonames.org/countrySubdivision?lat=#{lat}&lng=#{lon}&lang=#{lang}&username=#{Settings.geonames_username}")
+
+ # parse the response
+ response.elements.each("geonames/countrySubdivision") do |geoname|
+ name = geoname.text("adminName1")
+ country = geoname.text("countryName")
+
+ @results.push(:lat => lat, :lon => lon,
+ :zoom => Settings.geonames_zoom,
+ :name => name,
+ :suffix => ", #{country}")
+ end
+
+ render :action => "results"
+ rescue StandardError => e
+ @error = "Error contacting api.geonames.org: #{e}"
+ render :action => "error"
+ end
+
+ private
+
+ def fetch_text(url)
+ response = OSM.http_client.get(URI.parse(url))
+
+ if response.success?
+ response.body