Class: Decidim::Map::Provider::Geocoding::Here

Inherits:
Geocoding show all
Defined in:
lib/decidim/map/provider/geocoding/here.rb

Overview

The geocoding utility class for the HERE geocoding service

Instance Attribute Summary

Attributes inherited from Utility

#configuration, #locale, #organization

Instance Method Summary collapse

Methods inherited from Geocoding

#coordinates, #handle, #initialize, #search

Methods inherited from Utility

#initialize

Constructor Details

This class inherits a constructor from Decidim::Map::Geocoding

Instance Method Details

#address(coordinates, options = {}) ⇒ Object

See Also:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/decidim/map/provider/geocoding/here.rb', line 10

def address(coordinates, options = {})
  # Pass in a radius of 50 meters as an extra attribute for the HERE
  # API. Also sort the results by distance and pass a maxresults
  # attribute of 5.
  results = search(coordinates + [50], {
    params: {
      sortby: :distance,
      maxresults: 5
    }
  }.merge(options))
  return if results.empty?

  # Always prioritize house number results, even if they are not as
  # close as street level matches.
  hn_result = results.find do |r|
    r.data["resultType"] == "houseNumber"
  end
  return hn_result.address if hn_result

  # Some of the matches that have "resultType" == "street" do not even
  # contain the street name unless they also have the "streets" key in
  # the "scoring" -> "fieldScore" attribute defined.
  street_result = results.find do |r|
    r.data["scoring"]["fieldScore"].has_key?("streets")
  end
  return street_result.address if street_result

  # Otherwise, sort the results based on their exact distances from
  # the given coordinates (default functionality).
  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