Class: Geocoder::Result::Google
- Inherits:
-
Base
- Object
- Base
- Geocoder::Result::Google
show all
- Defined in:
- lib/geocoder/results/google.rb
Instance Attribute Summary
Attributes inherited from Base
#cache_hit, #data
Instance Method Summary
collapse
Methods inherited from Base
#initialize, #latitude, #longitude, #province, #province_code
Instance Method Details
#address(format = :full) ⇒ Object
10
11
12
|
# File 'lib/geocoder/results/google.rb', line 10
def address(format = :full)
formatted_address
end
|
#address_components ⇒ Object
98
99
100
|
# File 'lib/geocoder/results/google.rb', line 98
def address_components
@data['address_components']
end
|
#address_components_of_type(type) ⇒ Object
Get address components of a given type. Valid types are defined in Google’s Geocoding API documentation and include (among others):
:street_number
:locality
:neighborhood
:route
:postal_code
112
113
114
|
# File 'lib/geocoder/results/google.rb', line 112
def address_components_of_type(type)
address_components.select{ |c| c['types'].include?(type.to_s) }
end
|
#city ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/geocoder/results/google.rb', line 20
def city
fields = [:locality, :sublocality,
:administrative_area_level_3,
:administrative_area_level_2]
fields.each do |f|
if entity = address_components_of_type(f).first
return entity['long_name']
end
end
return nil end
|
#coordinates ⇒ Object
6
7
8
|
# File 'lib/geocoder/results/google.rb', line 6
def coordinates
['lat', 'lng'].map{ |i| geometry['location'][i] }
end
|
#country ⇒ Object
56
57
58
59
60
|
# File 'lib/geocoder/results/google.rb', line 56
def country
if country = address_components_of_type(:country).first
country['long_name']
end
end
|
#country_code ⇒ Object
62
63
64
65
66
|
# File 'lib/geocoder/results/google.rb', line 62
def country_code
if country = address_components_of_type(:country).first
country['short_name']
end
end
|
94
95
96
|
# File 'lib/geocoder/results/google.rb', line 94
def formatted_address
@data['formatted_address']
end
|
#geometry ⇒ Object
116
117
118
|
# File 'lib/geocoder/results/google.rb', line 116
def geometry
@data['geometry']
end
|
#neighborhood ⇒ Object
14
15
16
17
18
|
# File 'lib/geocoder/results/google.rb', line 14
def neighborhood
if neighborhood = address_components_of_type(:neighborhood).first
neighborhood['long_name']
end
end
|
#partial_match ⇒ Object
124
125
126
|
# File 'lib/geocoder/results/google.rb', line 124
def partial_match
@data['partial_match']
end
|
#place_id ⇒ Object
128
129
130
|
# File 'lib/geocoder/results/google.rb', line 128
def place_id
@data['place_id']
end
|
#postal_code ⇒ Object
68
69
70
71
72
|
# File 'lib/geocoder/results/google.rb', line 68
def postal_code
if postal = address_components_of_type(:postal_code).first
postal['long_name']
end
end
|
#precision ⇒ Object
120
121
122
|
# File 'lib/geocoder/results/google.rb', line 120
def precision
geometry['location_type'] if geometry
end
|
#route ⇒ Object
74
75
76
77
78
|
# File 'lib/geocoder/results/google.rb', line 74
def route
if route = address_components_of_type(:route).first
route['long_name']
end
end
|
#state ⇒ Object
32
33
34
35
36
|
# File 'lib/geocoder/results/google.rb', line 32
def state
if state = address_components_of_type(:administrative_area_level_1).first
state['long_name']
end
end
|
#state_code ⇒ Object
38
39
40
41
42
|
# File 'lib/geocoder/results/google.rb', line 38
def state_code
if state = address_components_of_type(:administrative_area_level_1).first
state['short_name']
end
end
|
#street_address ⇒ Object
86
87
88
|
# File 'lib/geocoder/results/google.rb', line 86
def street_address
[street_number, route].compact.join(' ')
end
|
#street_number ⇒ Object
80
81
82
83
84
|
# File 'lib/geocoder/results/google.rb', line 80
def street_number
if street_number = address_components_of_type(:street_number).first
street_number['long_name']
end
end
|
#sub_state ⇒ Object
44
45
46
47
48
|
# File 'lib/geocoder/results/google.rb', line 44
def sub_state
if state = address_components_of_type(:administrative_area_level_2).first
state['long_name']
end
end
|
#sub_state_code ⇒ Object
50
51
52
53
54
|
# File 'lib/geocoder/results/google.rb', line 50
def sub_state_code
if state = address_components_of_type(:administrative_area_level_2).first
state['short_name']
end
end
|
#types ⇒ Object
90
91
92
|
# File 'lib/geocoder/results/google.rb', line 90
def types
@data['types']
end
|