public
Description: Some personal changes I needed to acts_as_geocodable - see the original project at the homepage url below
Homepage: http://graticule.rubyforge.org/plugin.html
Clone URL: git://github.com/eliotsykes/acts_as_geocodable_extra.git
commit  92e9fa74db08460aa7876e06e97cf2196edc11d6
tree    34548cbb98527b4e196741110114dafab1d21967
parent  1efe9d6d3c7ed44c1da883d7fff1137b167a2c9c
name age message
file .gitignore Wed Jun 10 04:15:49 -0700 2009 Fixed weird problem with geocode.rb coordinates... [eliotsykes]
file CHANGELOG Thu Feb 26 00:11:13 -0800 2009 Improved documentation about changes acts_as_ge... [eliotsykes]
file MIT-LICENSE Sun Oct 01 22:08:33 -0700 2006 Updated README and added license information [daniel]
file README Thu Feb 26 00:11:13 -0800 2009 Improved documentation about changes acts_as_ge... [eliotsykes]
file Rakefile Sun Oct 01 19:05:53 -0700 2006 Initial commit of acts_as_geocodable [daniel]
file about.yml Tue Feb 13 06:33:02 -0800 2007 renamed meta.yml to about.yml [brandon]
directory generators/ Thu Feb 26 00:11:13 -0800 2009 Improved documentation about changes acts_as_ge... [eliotsykes]
file init.rb Fri Jul 27 09:17:26 -0700 2007 Moving stuff around [daniel]
file install.rb Sun Oct 01 19:05:53 -0700 2006 Initial commit of acts_as_geocodable [daniel]
directory lib/ Wed Jun 10 04:15:49 -0700 2009 Fixed weird problem with geocode.rb coordinates... [eliotsykes]
directory tasks/ Sun Oct 01 19:05:53 -0700 2006 Initial commit of acts_as_geocodable [daniel]
directory test/ Sat Oct 27 19:37:42 -0700 2007 allow plugin to be tested outside of a rails pr... [brandon]
file uninstall.rb Sun Oct 01 19:05:53 -0700 2006 Initial commit of acts_as_geocodable [daniel]
README
acts_as_geocodable_extra is some tweaks I've made to acts_as_geocodable that I've needed, 
see changelog for details. Original documentation below.



= acts_as_geocodable

acts_as_geocodable is a plugin to help build geo-aware applications. It automatically geocodes your models when they are 
saved, giving you the ability to search by location and calculate distances between records.

== Usage

  event = Event.create :street => "777 NE Martin Luther King, Jr. Blvd.",
    :locality => "Portland", :region => "Oregon", :postal_code => 97232
    
  event.geocode.latitude                                #=> 45.529100000000
  event.geocode.longitude                               #=> -122.644200000000
  
  event.distance_to "49423"                             #=> 1807.66560483205
  
  Event.find(:all, :within => 50, :origin => "97232")
  
  Event.find(:nearest, :origin => "Portland, OR")
  
== Upgrading

If you're upgrading from a previous version of this plugin, note that :city has been renamed to :locality to be 
consistent with Graticule 0.2.  Create a migration that has:

  rename_column :geocodes, :city, :locality

Also remember to change your mapping in your geocodable classes to use the :locality key instead of :city:

class Event < ActiveRecord::Base
  acts_as_geocodable :address => {:street => :address1, :locality => :city,
    :region => :state, :postal_code => :zip}
end

== Installation

Graticule[link:http://rubyforge.org/projects/graticule] is used for all the heavy lifting.

  gem install graticule --include-dependencies

Install the plugin

  script/plugin install -x http://source.collectiveidea.com/public/rails/plugins/acts_as_geocodable/

== Configuration

Create the required tables
  
  script/generate geocodable_migration add_geocodable_tables
  rake db:migrate

Set the default geocoder in your environment.rb file.

  Geocode.geocoder = Graticule.service(:yahoo).new 'your_api_key'

Then, in each model you want to make geocodable, add acts_as_geocodable.

  class Event < ActiveRecord::Base
    acts_as_geocodable
  end

The only requirement is that your model must have address fields. By default, acts_as_geocodable looks for attributes 
called +street+, +locality+, +region+, +postal_code+, and +country+.  To change these, you can provide a mapping in the 
<tt>:address</tt> option:

  class Event < ActiveRecord::Base
    acts_as_geocodable :address => {:street => :address1, :locality => :city, :region => :state, :postal_code => :zip}
  end

If that doesn't meet your needs, simply override the default +to_location+ method in your model, and return a 
Graticule::Location with those attributes set.

acts_as_geocodable can also update your address fields with the data returned from the geocoding service:

  class Event < ActiveRecord::Base
    acts_as_geocodable :normalize_address => true
  end

== IP-based Geocoding

acts_as_geocodable adds a remote_location method in your controllers that uses http://hostip.info to guess remote users 
location based on their IP address.

  def index
    @nearest = Store.find(:nearest, :origin => remote_location) if remote_location
    @stores = Store.find(:all)
  end

Keep in mind that IP-based geocoding is not always accurate, and often will not return any results.

== Development

The source code is available at:

  http://source.collectiveidea.com/public/rails/plugins/acts_as_geocodable/

Patches and suggestions are welcome!

== To Do

* Documentation!!!
* configurable formulas