trabajo-afectivo/lib/service/geo_location/gmaps.rb

51 lines
1.2 KiB
Ruby
Raw Normal View History

2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
2013-08-17 21:48:48 +00:00
class Service::GeoLocation::Gmaps
2012-04-29 20:47:35 +00:00
def self.geocode(address)
url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI.escape address}&sensor=true"
response = UserAgent.get(
url,
{},
{
open_timeout: 2,
read_timeout: 4,
total_timeout: 4,
verify_ssl: true,
},
)
return if !response.success?
2012-04-29 20:47:35 +00:00
result = JSON.parse(response.body)
2012-04-29 20:47:35 +00:00
2014-06-01 08:31:28 +00:00
return if !result
return if !result['results']
return if !result['results'].first
2012-04-29 20:47:35 +00:00
lat = result['results'].first['geometry']['location']['lat']
lng = result['results'].first['geometry']['location']['lng']
2019-06-28 11:38:49 +00:00
[lat, lng]
2012-04-29 20:47:35 +00:00
end
2015-04-27 14:53:29 +00:00
def self.reverse_geocode(lat, lng)
2012-04-29 20:47:35 +00:00
url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=#{lat},#{lng}&sensor=true"
response = UserAgent.get(
url,
{},
{
json: true,
open_timeout: 2,
read_timeout: 4,
total_timeout: 4,
verify_ssl: true,
},
)
return if !response.success?
2012-04-29 20:47:35 +00:00
result = JSON.parse(response.body)
result['results'].first['address_components'].first['long_name']
2012-04-29 20:47:35 +00:00
end
end