2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-08-17 21:48:48 +00:00
|
|
|
|
2015-07-15 19:45:40 +00:00
|
|
|
class Service::GeoLocation::Gmaps
|
2013-08-15 20:40:22 +00:00
|
|
|
|
2012-04-29 20:47:35 +00:00
|
|
|
def self.geocode(address)
|
2015-05-05 14:25:28 +00:00
|
|
|
url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI.escape address}&sensor=true"
|
2016-01-21 01:07:50 +00:00
|
|
|
response = UserAgent.get(
|
|
|
|
url,
|
|
|
|
{},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
open_timeout: 2,
|
|
|
|
read_timeout: 4,
|
2016-01-21 01:07:50 +00:00
|
|
|
total_timeout: 4,
|
|
|
|
},
|
|
|
|
)
|
2013-09-19 14:18:55 +00:00
|
|
|
return if !response.success?
|
2012-04-29 20:47:35 +00:00
|
|
|
|
2016-01-21 01:07:50 +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']
|
2015-04-27 14:53:29 +00:00
|
|
|
latlng = [lat, lng]
|
2012-04-29 20:47:35 +00:00
|
|
|
end
|
2013-08-15 20:40:22 +00:00
|
|
|
|
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"
|
2016-01-21 01:07:50 +00:00
|
|
|
response = UserAgent.get(
|
|
|
|
url,
|
|
|
|
{},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
json: true,
|
|
|
|
open_timeout: 2,
|
|
|
|
read_timeout: 4,
|
2016-01-21 01:07:50 +00:00
|
|
|
total_timeout: 4,
|
|
|
|
},
|
|
|
|
)
|
2013-09-19 14:18:55 +00:00
|
|
|
return if !response.success?
|
2012-04-29 20:47:35 +00:00
|
|
|
|
2016-01-21 01:07:50 +00:00
|
|
|
result = JSON.parse(response.body)
|
2013-08-15 20:40:22 +00:00
|
|
|
|
2012-04-29 20:47:35 +00:00
|
|
|
address = result['results'].first['address_components'].first['long_name']
|
2015-04-30 17:20:27 +00:00
|
|
|
address
|
2012-04-29 20:47:35 +00:00
|
|
|
end
|
2013-08-15 20:40:22 +00:00
|
|
|
end
|