1 2 3 4 5 6 7 8 9 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
require 'geocoder/results/base'
module Geocoder::Result
class Google < Base
def coordinates
['lat', 'lng'].map{ |i| geometry['location'][i] }
end
def address(format = :full)
formatted_address
end
def neighborhood
if neighborhood = address_components_of_type(:neighborhood).first
neighborhood['long_name']
end
end
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 # no appropriate components found
end
def state
if state = address_components_of_type(:administrative_area_level_1).first
state['long_name']
end
end
def state_code
if state = address_components_of_type(:administrative_area_level_1).first
state['short_name']
end
end
def sub_state
if state = address_components_of_type(:administrative_area_level_2).first
state['long_name']
end
end
def sub_state_code
if state = address_components_of_type(:administrative_area_level_2).first
state['short_name']
end
end
def country
if country = address_components_of_type(:country).first
country['long_name']
end
end
def country_code
if country = address_components_of_type(:country).first
country['short_name']
end
end
def postal_code
if postal = address_components_of_type(:postal_code).first
postal['long_name']
end
end
def route
if route = address_components_of_type(:route).first
route['long_name']
end
end
def street_number
if street_number = address_components_of_type(:street_number).first
street_number['long_name']
end
end
def street_address
[street_number, route].compact.join(' ')
end
def types
@data['types']
end
def formatted_address
@data['formatted_address']
end
def address_components
@data['address_components']
end
##
# 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
#
def address_components_of_type(type)
address_components.select{ |c| c['types'].include?(type.to_s) }
end
def geometry
@data['geometry']
end
def precision
geometry['location_type'] if geometry
end
def partial_match
@data['partial_match']
end
def place_id
@data['place_id']
end
def viewport
viewport = geometry['viewport'] || fail
bounding_box_from viewport
end
def bounds
bounding_box_from geometry['bounds']
end
private
def bounding_box_from(box)
return nil unless box
south, west = %w(lat lng).map { |c| box['southwest'][c] }
north, east = %w(lat lng).map { |c| box['northeast'][c] }
[south, west, north, east]
end
end
end
|