Class: JDX::Geocoder::Google
- Inherits:
-
Object
- Object
- JDX::Geocoder::Google
- 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
Instance Method Summary collapse
- #address ⇒ Object
-
#bounds ⇒ Object
- [northeast
-
[southwest]].
- #center ⇒ Object
- #city ⇒ Object
-
#coordinates ⇒ Object
coordinate order is longitude, latitude.
- #country ⇒ Object
- #country_code ⇒ Object
- #deg2rad(lng, lat) ⇒ Object
-
#initialize(term) ⇒ Google
constructor
A new instance of Google.
- #latitude ⇒ Object
- #longitude ⇒ Object
- #place_id ⇒ Object
- #postal_code ⇒ Object
-
#spherical_distance ⇒ Object
rubocop:disable Metrics/AbcSize.
- #state ⇒ Object
- #state_code ⇒ Object
- #types ⇒ Object
Methods included from Base
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
#address ⇒ Object
27 28 29 |
# File 'lib/jdx/geocoder/google.rb', line 27 def address data['formatted_address'] end |
#bounds ⇒ Object
- [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 |
#center ⇒ Object
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 |
#city ⇒ Object
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 |
#coordinates ⇒ Object
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 |
#country ⇒ Object
44 45 46 |
# File 'lib/jdx/geocoder/google.rb', line 44 def country type_find('country', 'long_name') end |
#country_code ⇒ Object
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 |
#latitude ⇒ Object
19 20 21 |
# File 'lib/jdx/geocoder/google.rb', line 19 def latitude data.dig('geometry', 'location', 'lat') end |
#longitude ⇒ Object
23 24 25 |
# File 'lib/jdx/geocoder/google.rb', line 23 def longitude data.dig('geometry', 'location', 'lng') end |
#place_id ⇒ Object
93 94 95 |
# File 'lib/jdx/geocoder/google.rb', line 93 def place_id data['place_id'] end |
#postal_code ⇒ Object
56 57 58 |
# File 'lib/jdx/geocoder/google.rb', line 56 def postal_code type_find('country', 'postal_code') end |
#spherical_distance ⇒ Object
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 |
#state ⇒ Object
40 41 42 |
# File 'lib/jdx/geocoder/google.rb', line 40 def state type_find('administrative_area_level_1', 'long_name') end |
#state_code ⇒ Object
48 49 50 |
# File 'lib/jdx/geocoder/google.rb', line 48 def state_code type_find('administrative_area_level_1', 'short_name') end |
#types ⇒ Object
89 90 91 |
# File 'lib/jdx/geocoder/google.rb', line 89 def types data['types'] end |