Git Product home page Git Product logo

acts_as_geocodable's Introduction

acts_as_geocodable

Gem Version Build Status Code Climate Code Coverage Dependency Status

acts_as_geocodable helps you 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.

Beginning with version 2, we require Rails 3. Use one of the 1.0.x tags to work with Rails 2.3.

We've adopted the ARel style syntax for finding 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.origin("97232", within: 50)

Event.origin("Portland, OR").nearest

Installation

Install as a gem

gem install acts_as_geocodable

or add it to your Gemfile

gem "acts_as_geocodable"

Graticule is used for all the heavy lifting and will be installed too.

Upgrading

Before October 2008, precision wasn't included in the Geocode model. Make sure you add a string precision column to your geocode table if you're upgrading from an older version, and update Graticule.

Also, 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

Configuration

Create the required tables

rails generate acts_as_geocodable
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 :address 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.origin(remote_location).nearest if remote_location
  @stores = Store.all
end

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

Contributing

In the spirit of free software, everyone is encouraged to help improve this project.

Here are some ways you can contribute:

  • using alpha, beta, and prerelease versions
  • reporting bugs
  • suggesting new features
  • writing or editing documentation
  • writing specifications
  • writing code (no patch is too small: fix typos, add comments, clean up inconsistent whitespace)
  • refactoring code
  • closing issues
  • reviewing patches

Submitting an Issue

We use the GitHub issue tracker to track bugs and features. Before submitting a bug report or feature request, check to make sure it hasn't already been submitted. You can indicate support for an existing issuse by voting it up. When submitting a bug report, please include a Gist that includes a stack trace and any details that may be necessary to reproduce the bug, including your gem version, Ruby version, and operating system. Ideally, a bug report should include a pull request with failing specs.

Submitting a Pull Request

  1. Fork the project.
  2. Create a topic branch.
  3. Implement your feature or bug fix.
  4. Add specs for your feature or bug fix.
  5. Run bundle exec rake. If your changes are not 100% covered and passing, go back to step 4.
  6. Commit and push your changes.
  7. Submit a pull request. Please do not include changes to the gemspec, version, or history file. (If you want to create your own version for some reason, please do so in a separate commit.)

To Do

  • configurable formulas

acts_as_geocodable's People

Contributors

adonaldson avatar bkeepers avatar bryckbost avatar danielmorrison avatar koos avatar laserlemon avatar latortuga avatar osaris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

acts_as_geocodable's Issues

Multiple geocoders?

Is it possible to use multiple geocoders? I've seen issues where a service won't recognize an address, even though it is valid, and another service will. Would be nice if we could fallback if the address isn't recognized. Is this possible?

Using distance calculations for relations?

Take the example on the site with Events. If Events habtm Locations and the Locations are what acts_as_geocodable, how do we go about searching for Events by geocode? Thanks!

Rails 3.1 Deprecation warning

Hi,

When I do :

acts_as_geocodable :address => {:street => :street_1, :locality => :city}

I get :

DEPRECATION WARNING: class_inheritable_attribute is deprecated, please use class_attribute method instead. Notice their behavior are slightly different, so refer to class_attribute documentation first.

With Rails 3.1.rc8 and ruby 1.9.2

Encoding::CompatibilityError if location returned by API contains accents

I have a geocodable model that fails to be saved with the following error (Rails 3, Ruby 1.9.2) :

Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT

After further investigations it looks that the problem comes from the answer of :

geocoder.locate(location)

which returns ASCII rather than UTF-8 and ruby fails with "l'Abb\xC3\xA9"

Rue de l'Abb\xC3\xA9 Chanlon\nF\xC3\xA9nay, Bourgogne 21600 FR

I don't know if this issue is more graticule related or it should be fixed in acts_as_geocodable ... but it only exists with Google API, Yahoo API seems to works fine.

ActiveModel compliance and usage?

Thanks for this plugin - it's really great.

I have a few instances where I need to mimic acts_as_geocodable behavior on ActiveModel compliant classes. I've introduced some hackery on my end to get around the scopes and the associations, etc.

My question is, would it be of interest for others if I abstracted things out a bit more so that scopes and other AR specifics were optionally loaded as part of the acts_as_geocodable call?

I'm thinking something along the lines of detecting if the class is an AR descendant or passing an argument to the method call like:

acts_as_geocodable :scopes_and_associations => false

NameError: uninitialized constant Graticule::Precision

Geocode.create.precision
NameError: uninitialized constant Graticule::Precision
from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:443:in load_missing_constant' from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:inrake_original_const_missing'
from /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2503:in const_missing' from /Library/Ruby/Gems/1.8/gems/acts_as_geocodable-1.0.2/lib/acts_as_geocodable/geocode.rb:38:inprecision'
from (irb):11

I run Rails 2.3.5, acts_as_geocodable 1.0.2 and Graticule 0.2.12

Rails 3 changing Activerecord glitches with includes

I'm using rails 3 and am using this gem for a project of mine.

This works
Organization.joins(:skills).where("skills.id IN (1,2,3,4,5,6)").origin("17522", :within => 20)

This seems to work but then gives a NoMethodError: undefined method `distance'
Organization.includes(:skills).where("skills.id IN (1,2,3,4,5,6)").origin("17601", :within => 20)

Any idea how to get the includes working with acts_as_geocodable?

Yandex API Geocoder problem

There seem to be an issue with Yandex Maps API integration. When I try to get coordinates for the same address, but typed in different ways, e.g. (addressed are for Санкт-Петербург):

  1. лиговский 74 (works)
  2. лиговский, 74 (works)
  3. лиговский проспект, 74 (works)
  4. лиговский проспект 74 (returns exception)

though direct query to Yandex API return correct result with lat/long coordinates http://geocode-maps.yandex.ru/1.x/?geocode=Санкт-Петербург,+лиговский+проспект+74

super: no superclass method `precision='

Encountering "super: no superclass method `precision='" on Rails 2.3.2 when looking up a geocode that hasn't been looked up before.

I put a rescue block around that in my fork for the time being.

[Rails 2.3.x] undefined method locate

Hi everybody,
I have a simple class Store that acts_as_geocodable, in Rails 2.3.x, as shown below:

acts_as_geocodable :address => {:street => :address, :locality => :locality_name, :region => :region_name}

def region_name
   region.name  || ''
end

def locality_name
   city || province.name
end

:address is an ActiveRecord field. I'd like to run such a query:

Store.find(:all, :within => 10, :origin => "80026")

to find all stores matching that origin. But I get this error:

*NoMethodError: undefined method locate' for nil:NilClass from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/whiny_nil.rb:52:inmethod_missing'
from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/acts_as_geocodable-1.0.0/lib/acts_as_geocodable/geocode.rb:23:in create_by_query' from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/acts_as_geocodable-1.0.0/lib/acts_as_geocodable/geocode.rb:19:infind_or_create_by_query'
from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/acts_as_geocodable-1.0.0/lib/acts_as_geocodable.rb:134:in location_to_geocode' from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/acts_as_geocodable-1.0.0/lib/acts_as_geocodable.rb:86:infind'
from (irb):10

Deprecation Warning InstanceMethods

Hi,

I have a warning deprecation with acts_as_geocodable.

At this line : acts_as_geocodable :address => {:street => :street_1, :locality => :city, :postal_code => :postal_code, :country => :country}

This Warning : DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in Establishment instead.

With rails 3.2 and ruby 1.9.3

Rails 3 generators

I had some issues when running the generators in Rails 3.0beta4

It says, "could not find generator geocodable_migration."

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.