Skip to content

Instantly share code, notes, and snippets.

@joshnesbitt
Created April 19, 2011 07:26
Show Gist options
  • Select an option

  • Save joshnesbitt/926961 to your computer and use it in GitHub Desktop.

Select an option

Save joshnesbitt/926961 to your computer and use it in GitHub Desktop.
gem 'geokit'
# This is an initialiser which sets up GeoKit
if defined? Geokit
Geokit::default_units = :miles
Geokit::default_formula = :sphere
Geokit::Geocoders::request_timeout = 3
Geokit::Geocoders::proxy_addr = nil
Geokit::Geocoders::proxy_port = nil
Geokit::Geocoders::proxy_user = nil
Geokit::Geocoders::proxy_pass = nil
Geokit::Geocoders::yahoo = 'REPLACE_WITH_YOUR_YAHOO_KEY'
Geokit::Geocoders::google = 'REPLACE_WITH_YOUR_GOOGLE_KEY'
Geokit::Geocoders::geocoder_us = false
Geokit::Geocoders::geocoder_ca = false
Geokit::Geocoders::provider_order = [:google, :us]
Geokit::Geocoders::ip_provider_order = [:geo_plugin, :ip]
end
You'll then have access to the GeoLoc object where you can access
location data such as #lat and #lng.
= debug @tour_date.geo.lat
= debug @tour_date.geo.lng
# Here is an example of a simple GeoKit logging function.
# If a query is present we try to find a location from it
# and return an unsaved record.
class TourDate < ActiveRecord::Base
validates :geo, :presence => true
serialize :geo, Geokit::GeoLoc
class << self
def log(params = {})
if query = params.delete(:query)
location = Geokit::Geocoders::MultiGeocoder.geocode(query)
params.merge!(:geo => location) if location.success
end
new(params)
end
end
end
# The tour dates controller expects a params hash just
# like a standard Rails form. If the query isn't present
# we can fall back to an alternative, such as the requesting IP address.
class TourDatesController < ApplicationController
def create
unless params[:tour_date][:query].present?
params[:tour_date][:query] = request.ip
end
@tour_date = TourDate.log(params[:tour_date])
if @tour_date.save
redirect_to tour_date_path(@tour_date), :notice => "Successfully created tour date."
else
render :new
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment