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
|
# File 'lib/google_geocoder/google_geocoder.rb', line 42
def query(location)
raise(MissingLocation) if location.nil?
request = "#{URL_STRING}/json?address=#{CGI.escape(location)}&sensor=false"
begin
response = call_service(request)
raise(GeocoderServerIsDown) if response.empty?
if response['status'] == "ZERO_RESULTS"
[]
else
raise(InvalidResponse, response['results']) if response['status'] != "OK"
root = response["results"][0]
address_components = root["address_components"]
geometry = root["geometry"]
country = address_component(address_components, "country")
locality = address_component(address_components, "locality")
administrative_area_l1 = address_component(address_components, "administrative_area_level_1")
administrative_area_l2 = address_component(address_components, "administrative_area_level_2")
latitude = geometry["location"]["lat"]
longitude = geometry["location"]["lng"]
puts "Country: #{country['long_name']} (#{country['short_name']})"
puts "Locality: #{locality['long_name']} (#{locality['short_name']})"
puts "Administrative Area (level 1): #{administrative_area_l1['long_name']} (#{administrative_area_l1['short_name']})"
puts "Administrative Area (level 2): #{administrative_area_l2['long_name']} (#{administrative_area_l2['short_name']})" unless administrative_area_l2.nil?
puts "Latitude: #{latitude}"
puts "Longitude: #{longitude}"
[latitude, longitude]
end
rescue OpenURI::HTTPError
raise(GeocoderServerIsDown)
end
end
|