Skip to content

Instantly share code, notes, and snippets.

@michaelohm
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save michaelohm/04948b040b96dbbf22e1 to your computer and use it in GitHub Desktop.

Select an option

Save michaelohm/04948b040b96dbbf22e1 to your computer and use it in GitHub Desktop.

Revisions

  1. michaelohm revised this gist Jun 10, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Geocoder.md
    Original file line number Diff line number Diff line change
    @@ -90,7 +90,10 @@ after_validation :geocode


    #Installation

    gem install geocoder

    Geocoder requires :latitude and :longitude attributes to be added to the database columns

    #Railscast
    http://railscasts.com/episodes/273-geocoder
  2. michaelohm revised this gist Jun 10, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Geocoder.md
    Original file line number Diff line number Diff line change
    @@ -77,8 +77,9 @@ nearbys = Place.near("Omaha, NE", 50,
    bearing = nearbys.first.bearing # => 46.12
    Geocoder::Calculations.compass_point(
    bearing) # => "NE"
    The bearing attribute is a number between 0 and 360 indicating the number of degrees clockwise from due North (eg: 180 = South, 270 = West).
    ```
    The bearing attribute is a number between 0 and 360 indicating the number of degrees clockwise from due North (eg: 180 = South, 270 = West).

    Given a Venue model with known street address, automatically fetch coordinates after validation and store in latitude and longitude attributes:

    ```ruby
  3. michaelohm renamed this gist Jun 10, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. michaelohm created this gist Jun 10, 2014.
    95 changes: 95 additions & 0 deletions Geocoder
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    #What is it?
    Geocoder lets you look up street addresses, IP addresses and geographic coordinates. It also allows geographic queries using objects, such as looking up hotels, events, restaurants, etc near a city of your choosing.

    #Features

    source: http://www.rubygeocoder.com/

    Simple Geocoding by Street Address

    Given a Venue model with known street address, automatically fetch coordinates after validation and store in latitude and longitude attributes:

    ```ruby
    # app/models/venue.rb
    geocoded_by :address
    after_validation :geocode
    ```

    Simple Geocoding by IP Address

    Given a User model with known IP address, automatically fetch coordinates and store in lat and lon attributes:

    ```ruby
    # app/models/user.rb
    geocoded_by :ip_address,
    :latitude => :lat, :longitude => :lon
    after_validation :geocode
    ```

    Simple Reverse Geocoding by Coordinates

    Given a Place model with known latitude/longitude coordinates, automatically fetch address and store in location attribute (stored in address attribute if :address option omitted):
    ```ruby
    # app/models/place.rb
    reverse_geocoded_by :latitude, :longitude,
    :address => :location
    after_validation :reverse_geocode
    ```
    Only Geocode When Attributes Have Changed

    Only look up coordinates if address changed since last save:
    ```ruby
    # app/models/venue.rb
    geocoded_by :address
    after_validation :geocode,
    :if => lambda{ |obj| obj.address_changed? }
    ```
    Custom Handling of Detailed Reverse Geocoding Results

    Given a Place model with known latitude/longitude coordinates, automatically fetch address components and store in separate attributes:

    ```ruby
    # app/models/place.rb
    reverse_geocoded_by :lat, :lon do |obj,results|
    if geo = results.first
    obj.city = geo.city
    obj.zipcode = geo.postal_code
    obj.country = geo.country_code
    end
    end
    after_validation :reverse_geocode
    ```
    Forward and Reverse Geocoding on Same Model

    Given a Place model, objects of which sometimes have a street address and sometimes have coordinates, automatically fetch and fill in whatever's missing, based on what's provided:
    ```ruby
    # app/models/place.rb
    geocoded_by :address
    reverse_geocoded_by :latitude, :longitude
    after_validation :geocode, :reverse_geocode
    ```
    Find the Bearing (Direction) Between Places

    Given a Place model with geocoded objects, find the distance and direction of the closest ones:
    ```ruby
    nearbys = Place.near("Omaha, NE", 50,
    :order => "distance")
    bearing = nearbys.first.bearing # => 46.12
    Geocoder::Calculations.compass_point(
    bearing) # => "NE"
    The bearing attribute is a number between 0 and 360 indicating the number of degrees clockwise from due North (eg: 180 = South, 270 = West).
    ```
    Given a Venue model with known street address, automatically fetch coordinates after validation and store in latitude and longitude attributes:

    ```ruby
    # app/models/venue.rb
    geocoded_by :address
    after_validation :geocode
    ```


    #Installation
    gem install geocoder

    #Railscast
    http://railscasts.com/episodes/273-geocoder