Return to Snippet

Revision: 20211
at November 9, 2009 19:33 by PesPes


Updated Code
require 'net/http'
require 'uri'

class Gcode
  
  def initialize
    @g_url = "http://maps.google.com/maps/geo?q=" 
    @output = "&output=csv"
    @key = " [API KEY HERE] "
  end
  
  def g_code(loc)
    query = @g_url << loc << @output << @key
    query.to_s
    response = Net::HTTP.get_response URI.parse(query)
    fields = response.body.split(',')
    if fields[0] = 200
      latlong = {"x" => fields[2], "y" => fields[3] }
      return latlong
    else
      puts "sorry, address cannot be geocoded"
    end
  end

end

Revision: 20210
at November 9, 2009 19:30 by PesPes


Initial Code
require 'net/http'
require 'uri'

class Gcode
  
  def initialize
    @g_url = "http://maps.google.com/maps/geo?q=" 
    @output = "&output=csv"
    @key = " [API KEY HERE] "
  end
  
  def g_code(loc)
    query = @g_url << loc << @output << @key
    query.to_s
    response = Net::HTTP.get_response URI.parse(query)
    fields = response.body.split(',')
    if fields[0] = 200
      lat = fields[2]
      long = fields[3]
      latlon = {"x" => lat, "y" => long }
      return latlon
    else
      puts "sorry, address cannot be geocoded"
    end
  end

end

Initial URL


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

Initial Title
Google HTTP geocoding class

Initial Tags


Initial Language
Ruby