Class: Decidim::Map::Geocoding

Inherits:
Utility
  • Object
show all
Defined in:
decidim-core/lib/decidim/map/geocoding.rb

Overview

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

Instance Attribute Summary

Attributes inherited from Utility

#configuration, #locale, #organization

Instance Method Summary collapse

Constructor Details

#initialize(organization:, config:, locale: I18n.locale.to_s) ⇒ Geocoding

Returns a new instance of Geocoding.

See Also:


9
10
11
12
# File 'decidim-core/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.

Parameters:

  • 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

  • options (Hash) (defaults to: {})

    Extra options to be provided for the geocoder search

Returns:

  • (String, nil)

    The corresponding address found by the geocoder or nil when no result was found


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'decidim-core/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 = {}) ⇒ 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.

Parameters:

  • address (String)

    The address to search the geocoordinates for

  • options (Hash) (defaults to: {})

    Extra options to be provided for the geocoder search

Returns:

  • (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


50
51
52
# File 'decidim-core/lib/decidim/map/geocoding.rb', line 50

def coordinates(address, options = {})
  Geocoder.coordinates(address, geocoder_options(options))
end

#handleSymbol

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.

Returns:

  • (Symbol)

    The handle of the geocoder gem's lookup


18
19
20
# File 'decidim-core/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.

Parameters:

  • query (String, Array(<Float, String>, <Float, String>))

    The search query to be passed to the geocoder

  • options (Hash) (defaults to: {})

    Extra options to be provided for the geocoder search

Returns:

  • (Array(Float, Float), String, nil)

    The corresponding result for the search query, either a string or an array containing the result coordinates or nil when no corresponding result was found.


33
34
35
# File 'decidim-core/lib/decidim/map/geocoding.rb', line 33

def search(query, options = {})
  Geocoder.search(query, geocoder_options(options))
end