Skip to content

Instantly share code, notes, and snippets.

@bulters
Last active December 14, 2015 18:59
Show Gist options
  • Save bulters/5133207 to your computer and use it in GitHub Desktop.
Save bulters/5133207 to your computer and use it in GitHub Desktop.
Quick Google Geocoding Micro Gem

GGCode

GGCode is a small Google Maps API Geocoder using the Google Maps API v3.

Usage is extremely easy:

  1. Add to your gemfile

    gem 'ggcode', :git => 'https://gist.github.com/bulters/5133207'

  2. bundle

Reason I'm using this micro-gem is that the Graticule gem does not support the Google Maps API v3 and v2 is recently retired (8-3-2013).

Gem::Specification.new do |s|
s.name = 'ggcode'
s.summary = ''
s.description = ''
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
s.files = ['ggcode.rb']
s.require_path = '.'
s.author = 'Jeroen Bulters'
s.email = '[email protected]'
s.homepage = 'http://holder.nl'
s.add_development_dependency('httparty', ["~> 0.10.2"])
end
class GGCode
BASE_GOOGLE_URI = "https://maps.googleapis.com/maps/api/geocode/json"
def self.geocode(address)
url = BASE_GOOGLE_URI + "?address=#{ CGI.escape(address) }&sensor=false"
response = HTTParty.get(url)
if response.code == 200 && response.parsed_response && response.parsed_response["results"].length > 0
location = response.parsed_response["results"][0]["geometry"]["location"]
{:latitude => location["lat"], :longitude => location["lng"] }
else
nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment