Class: JDX::Geocoder::Google

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/jdx/geocoder/google.rb

Constant Summary collapse

RADIUS =
6371
CITY_FIELDS =
%w[locality sublocality administrative_area_level_2 administrative_area_level_3].freeze

Instance Attribute Summary

Attributes included from Base

#data

Instance Method Summary collapse

Methods included from Base

#search

Constructor Details

#initialize(term) ⇒ Google

Returns a new instance of Google.



10
11
12
# File 'lib/jdx/geocoder/google.rb', line 10

def initialize(term)
  @term = term
end

Instance Method Details

#addressObject



27
28
29
# File 'lib/jdx/geocoder/google.rb', line 27

def address
  data['formatted_address']
end

#boundsObject

[northeast
southwest]


61
62
63
64
65
66
67
68
69
70
# File 'lib/jdx/geocoder/google.rb', line 61

def bounds
  @bounds ||= begin
    bounds = data.dig('geometry', 'bounds') || data.dig('geometry', 'viewport')
    return nil if bounds.nil?

    bounds.values.map do |v|
      v.values.reverse!
    end
  end
end

#centerObject



72
73
74
75
76
77
# File 'lib/jdx/geocoder/google.rb', line 72

def center
  @center ||= begin
    co1, co2 = bounds
    [(co1[0] + co2[0]) / 2, (co1[1] + co2[1]) / 2]
  end
end

#cityObject



31
32
33
34
35
36
37
38
# File 'lib/jdx/geocoder/google.rb', line 31

def city
  CITY_FIELDS.each do |c|
    if (r = type_find(c, 'long_name'))
      return r
    end
  end
  nil
end

#coordinatesObject

coordinate order is longitude, latitude



15
16
17
# File 'lib/jdx/geocoder/google.rb', line 15

def coordinates
  data.dig('geometry', 'location').values.reverse!
end

#countryObject



44
45
46
# File 'lib/jdx/geocoder/google.rb', line 44

def country
  type_find('country', 'long_name')
end

#country_codeObject



52
53
54
# File 'lib/jdx/geocoder/google.rb', line 52

def country_code
  type_find('country', 'short_name')
end

#deg2rad(lng, lat) ⇒ Object



85
86
87
# File 'lib/jdx/geocoder/google.rb', line 85

def deg2rad(lng, lat)
  [lng * Math::PI / 180, lat * Math::PI / 180]
end

#latitudeObject



19
20
21
# File 'lib/jdx/geocoder/google.rb', line 19

def latitude
  data.dig('geometry', 'location', 'lat')
end

#longitudeObject



23
24
25
# File 'lib/jdx/geocoder/google.rb', line 23

def longitude
  data.dig('geometry', 'location', 'lng')
end

#place_idObject



93
94
95
# File 'lib/jdx/geocoder/google.rb', line 93

def place_id
  data['place_id']
end

#postal_codeObject



56
57
58
# File 'lib/jdx/geocoder/google.rb', line 56

def postal_code
  type_find('country', 'postal_code')
end

#spherical_distanceObject

rubocop:disable Metrics/AbcSize



79
80
81
82
83
# File 'lib/jdx/geocoder/google.rb', line 79

def spherical_distance # rubocop:disable Metrics/AbcSize
  rlng1, rlat1 = deg2rad(*bounds[1])
  rlng2, rlat2 = deg2rad(*center)
  (2 * RADIUS * Math.asin(Math.sqrt(Math.sin((rlat2 - rlat1) / 2)**2 + Math.cos(rlat1) * Math.cos(rlat2) * Math.sin((rlng2 - rlng1) / 2)**2))).round(3)
end

#stateObject



40
41
42
# File 'lib/jdx/geocoder/google.rb', line 40

def state
  type_find('administrative_area_level_1', 'long_name')
end

#state_codeObject



48
49
50
# File 'lib/jdx/geocoder/google.rb', line 48

def state_code
  type_find('administrative_area_level_1', 'short_name')
end

#typesObject



89
90
91
# File 'lib/jdx/geocoder/google.rb', line 89

def types
  data['types']
end