Tuesday, April 5, 2011

Rails3 + GeoKit (part I)

GeoKit is a great plugin by Andrew Lewis, which can calculate distance between two points on the earth, search points in your database within a specified distance from an origin, geocoding from multiple providers (convert address to (lat,lng) or vice versa),and find the city,lat,lng of an IP address. Clear explanations: here
Since I am using Rails3, I can not use the old plugin of Geokit. I found many tutorials on how to install Geokit, but most are for the previous version.
Here is page of the new version of Geokit for Rails3. this, However, not all the functions in the old version of Geokit has been updated.

Follow the installation guide

1. put this line gem 'geokit-rails3' in the Gemfile
2. execute bundle install

Then the installation is done

In the code, add acts_as_mappable to an ActiveRecord::Base

class Location < ActiveRecord::Base
acts_as_mappable
end

Then in a controller, you can play with it.
geoencoing example:

@res=MultiGeocoder.geocode('6224 5th Ave. Pittsburgh, PA')
@rev=GoogleGeocoder.reverse_geocode([40.4527037, -79.9219421])

In the view
The (lat,lng) of 6224 5th Ave. Pittsburgh, PA
lat : <%=@res.lat%>
lng : <%=@res.lng%>
The address of
(lat,lng) = (40.4527037, -79.9219421)
<%=@rev.full_address%>


GeoKit for Rails3
GeoKit for previous Rails

No comments:

Post a Comment