DistanceFox

DistanceFox was extracted from our Rails API at GoFoxBox. DistanceFox calculates the driving distance from a given destination to the nearest of multiple warehouses. We use the tool at the checkout of our shop for real-time calculations of distance based shipping rates. For fast calculations the tool finds the nearest warehouse location based on the harversine distance formula, which calculates distances based on the coordinates and earth radius. For accuracy the driving distance to the nearest warehouse will be fetched with the Google Distance Matrix API. This has proven to be a very accurate and fast way to find the nearest location across multiple warehouses without losing accuracy in the result.

Installation

Add this line to your application's Gemfile:

gem 'distance_fox'

And then execute:

$ bundle

Or install it yourself as:

$ gem install distance_fox

Configuration

Activate "Distance Matrix API" & "Geocoding API" in your Google Developers Console.

Set up DistanceFox with your API Key:

DistanceFox.configure do |config|
  config.api_key = 'YOUR_API_KEY'
end

Usage

# Origin can be:
# * an array of coordinates ([lat,lon])
# * an array of coordinates (['lat','lon'])
# * a geocodable address (string)
#
# Destinations can be:
# * an array of hashes ([{'city' => 'City', 'coordinates' => ['lat','lon']}])
DistanceFox::Calculations.nearest_driving_distance(origin_coordinates, destinations)

# Origin can be:
# * an array of float coordinates ([lat,lon])
# * an array of string coordinates (['lat','lon'])
#
# Destinations can be:
# * an array of hashes ([{'city' => 'City', 'coordinates' => ['lat','lon']}])
DistanceFox::Calculations.nearest_linear_distance(origin, destinations)

# Origin and destination can be:
#
# * an array of coordinates ([lat,lon])
# * an array of coordinates (['lat','lon'])
# * a geocodable address (string)
DistanceFox::Calculations.driving_distance(origin, destination)

# Origin and destination can be:
#
# * an array of coordinates ([lat,lon])
# * an array of coordinates (['lat','lon'])
# * a geocodable address (string)
DistanceFox::Calculations.linear_distance(origin, destination)

# Address can be:
# a geocodable address (string)
DistanceFox::Calculations.geocode(address)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/danielwollen/distance_fox. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the DistanceFox project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.