Created
April 19, 2011 07:26
-
-
Save joshnesbitt/926961 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gem 'geokit' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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