Class: Decidim::Map::Geocoding
- Defined in:
- lib/decidim/map/geocoding.rb
Overview
A base class for geocoding functionality, common to all geocoding services.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Utility
#configuration, #locale, #organization
Instance Method Summary collapse
-
#address(coordinates, options = {}) ⇒ String?
Does a reverse geocoding request with the given geocoordinates (latitude/longitude) coordinates and returns a clear text address for the closest result.
-
#coordinates(address, options = {}) ⇒ Array(Float, Float)?
Does a geocoding request with the given address and returns the corresponding geocoordinates as an array where the first element corresponds the latitude and the second element corresponds to the longitude.
-
#handle ⇒ Symbol
The “lookup” handle to be passed to the geocoder gem.
-
#initialize(organization:, config:, locale: I18n.locale.to_s) ⇒ Geocoding
constructor
A new instance of Geocoding.
-
#search(query, options = {}) ⇒ Array(Float, Float), ...
A common search method to lookup information about an address or coordinates from the geocoder.
Constructor Details
#initialize(organization:, config:, locale: I18n.locale.to_s) ⇒ Geocoding
Returns a new instance of Geocoding.
9 10 11 12 |
# File 'lib/decidim/map/geocoding.rb', line 9 def initialize(organization:, config:, locale: I18n.locale.to_s) super prepare! end |
Instance Method Details
#address(coordinates, options = {}) ⇒ String?
Does a reverse geocoding request with the given geocoordinates (latitude/longitude) coordinates and returns a clear text address for the closest result.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/decidim/map/geocoding.rb', line 66 def address(coordinates, = {}) results = search(coordinates, ) return if results.empty? results.sort! do |result1, result2| dist1 = Geocoder::Calculations.distance_between( result1.coordinates, coordinates ) dist2 = Geocoder::Calculations.distance_between( result2.coordinates, coordinates ) dist1 <=> dist2 end results.first.address end |
#coordinates(address, options = {}) ⇒ Array(Float, Float)?
Does a geocoding request with the given address and returns the corresponding geocoordinates as an array where the first element corresponds the latitude and the second element corresponds to the longitude.
50 51 52 |
# File 'lib/decidim/map/geocoding.rb', line 50 def coordinates(address, = {}) Geocoder.coordinates(address, ()) end |
#handle ⇒ Symbol
The “lookup” handle to be passed to the geocoder gem. For the full list, see github.com/alexreisner/geocoder/blob/master/README_API_GUIDE.md.
18 19 20 |
# File 'lib/decidim/map/geocoding.rb', line 18 def handle @handle ||= self.class.name.to_s.split("::")[-1].underscore.to_sym end |
#search(query, options = {}) ⇒ Array(Float, Float), ...
A common search method to lookup information about an address or coordinates from the geocoder.
33 34 35 |
# File 'lib/decidim/map/geocoding.rb', line 33 def search(query, = {}) Geocoder.search(query, ()) end |