gmaps.rb 955 B

12345678910111213141516171819202122232425262728293031
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. class GeoLocation::Gmaps
  3. def self.geocode(address)
  4. url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI::escape address}&sensor=true"
  5. response = UserAgent.request(url)
  6. return if !response.success?
  7. result = JSON.parse( response.body )
  8. return if !result
  9. return if !result['results']
  10. return if !result['results'].first
  11. lat = result['results'].first['geometry']['location']['lat']
  12. lng = result['results'].first['geometry']['location']['lng']
  13. latlng = [lat,lng]
  14. end
  15. def self.reverse_geocode(lat,lng)
  16. url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=#{lat},#{lng}&sensor=true"
  17. response = UserAgent.request(url)
  18. return if !response.success?
  19. result = JSON.parse( response.body )
  20. address = result['results'].first['address_components'].first['long_name']
  21. return address
  22. end
  23. end