What can Geokit do for you?
- Distance calculations between two points on the earth. Calculate the distance in miles or KM, with all the trigonometry abstracted away by Geokit.
- ActiveRecord distance-based finders. For example, you can find all the points in your database within a 50-mile radius.
- Geocoding from multiple providers. It supports Google, Yahoo, Geocoder.us, Geonames, Geocoder.ca, and more. Geokit provides a uniform response structure from all of them. It also provides a fail-over mechanism, in case your input fails to geocode in one service.
- IP-based location lookup utilizing hostip.info. Provide an IP address, and get city name and latitude/longitude in return.
- A before_filter helper to geocode the user's location based on IP address, and retain the location in a cookie.
Examples?
Find near latitude and longitude:
Store.find(:all, :origin =>[37.792,-122.393], :within=>10)
Find near an address:
Store.find(:all, :origin=>'100 Spear st, San Francisco, CA', :within=>10)
Order by distance from the center of a zipcode:
Store.find(:all, :origin=>'94117', :within=>10,
:order=>'distance asc')
Combine distance conditions with regular conditions
Store.find(:all, :origin=>'94117', :within=>10,
:conditions=>{:store_type=>'CAFE'})
Geocode an address:
include Geokit::Geocoders
res=MultiGeocoder.geocode('100 Spear st, San Francisco, CA')
puts res.ll # ll=latitude,longitude
Find the address near a latitude/longitude (reverse geocoding):
include Geokit::Geocoders
res=GoogleGeocoder.reverse_geocode([37.792821,-122.393992])
puts res.full_address
>> 36-98 Mission St, San Francisco, CA 94105, USA
Find distances, headings, endpoints, and midpoints:
distance=home.distance_from(work, :units=>:miles)
heading=home.heading_to(work) # result is in degrees, 0 is north
endpoint=home.endpoint(90,2) # two miles due east
midpoing=home.midpoint_to(work)
Test if a point is contained within bounds:
bounds=Bounds.new(sw_point,ne_point)
bounds.contains?(home)
Find within bounds:
bounds=Bounds.new(sw_point,ne_point)
Store.find :all, :bounds=>bounds
Find distance to a second location with on-the-fly geocoding:
s = Store.find(:first)
distance = s.distance_from('100 spear st, San Francisco, CA')
Ready to Install?
1. Install the Rails plugin:
cd [YOUR_RAILS_APP_ROOT]
script/plugin install git://github.com/andre/geokit-rails.git
2. Add this line to your config/environment.rb
(inside the Rails::Initializer.run do |config| block)
config.gem "geokit"
This informs Rails of the gem dependency.
3. Tell Rails to install the gem:
rake gems:install
And you're good to go! If you're running an older verion of Rails, just install the gem manually: sudo gem install geokit
If you're having trouble with dependencies ....
Try installing the gem manually, then adding a require 'geokit' to the top of vendor/plugins/geokit-rails/init.rb and/or config/geokit_config.rb.
Need Some Help?
Join the Geokit Google group. Others may have had the same problem, or can help answer your question.
