Google HTTP geocoding class


/ Published in: Ruby
Save to your folder(s)

pass in the loc variable to g_code as a string. It doesn't do any sort of error-checking on the string however.


Copy this code and paste it in your HTML
  1. require 'net/http'
  2. require 'uri'
  3.  
  4. class Gcode
  5.  
  6. def initialize
  7. @g_url = "http://maps.google.com/maps/geo?q="
  8. @output = "&output=csv"
  9. @key = " [API KEY HERE] "
  10. end
  11.  
  12. def g_code(loc)
  13. query = @g_url << loc << @output << @key
  14. query.to_s
  15. response = Net::HTTP.get_response URI.parse(query)
  16. fields = response.body.split(',')
  17. if fields[0] = 200
  18. latlong = {"x" => fields[2], "y" => fields[3] }
  19. return latlong
  20. else
  21. puts "sorry, address cannot be geocoded"
  22. end
  23. end
  24.  
  25. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.