class Decidim::Map::Geocoding

A base class for geocoding functionality, common to all geocoding services.

Public Class Methods

new(organization:, config:, locale: I18n.locale.to_s) click to toggle source

@see Decidim::Map::Utility#initialize

Calls superclass method Decidim::Map::Utility::new
# File lib/decidim/map/geocoding.rb, line 9
def initialize(organization:, config:, locale: I18n.locale.to_s)
  super
  prepare!
end

Public Instance Methods

address(coordinates, options = {}) click to toggle source

Does a reverse geocoding request with the given geocoordinates (latitude/longitude) coordinates and returns a clear text address for the closest result.

@param coordinates [Array(<Float, String>, <Float, String>)] An array of

the coordinates where the first element is the latitude and the second
element is the longitude

@param options [Hash] Extra options to be provided for the geocoder

search

@return [String, nil] The corresponding address found by the geocoder

or nil when no result was found
# File lib/decidim/map/geocoding.rb, line 66
def address(coordinates, options = {})
  results = search(coordinates, options)
  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 = {}) click to toggle source

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.

@param address [String] The address to search the geocoordinates for @param options [Hash] Extra options to be provided for the geocoder

search

@return [Array(Float, Float), nil] The corresponding coordinates found

by the geocoder where the first element corresponds to the latitude
coordinate and the second element corresponds to the longitude
coordinate or nil when no result was found
# File lib/decidim/map/geocoding.rb, line 50
def coordinates(address, options = {})
  Geocoder.coordinates(address, geocoder_options(options))
end
handle() click to toggle source

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.

@return [Symbol] The handle of the geocoder gem's lookup

# File lib/decidim/map/geocoding.rb, line 18
def handle
  @handle ||= self.class.name.to_s.split("::")[-1].underscore.to_sym
end

Protected Instance Methods

configure!(config) click to toggle source

@see Decidim::Map::Utility#configure

# File lib/decidim/map/geocoding.rb, line 88
def configure!(config)
  @configuration = config.merge(
    http_headers: { "Referer" => organization.host }
  )
end
geocoder_options(options) click to toggle source

Prepares the options to be passed to the Geocoder gem.

@param options [Hash] The options hash to be passed to Geocoder.

@return [Hash]

# File lib/decidim/map/geocoding.rb, line 99
def geocoder_options(options)
  { lookup: handle, language: locale }.merge(options)
end

Private Instance Methods

prepare!() click to toggle source

Prepares the geocoder for the lookups with the configured geocoding service. This is called when an instance of the geocoding utility is created.

@return [void]

# File lib/decidim/map/geocoding.rb, line 110
def prepare!
  Geocoder.configure(handle => configuration)
end