== Geosack On Rails
This plugin enables you to utilize geosack.com unique geocode caching service. Geosack provides a persistent api that caches google-map/geocode lookups - enabling you to re-request the same information any number of times you like without exceeding your quota's or rate-limits from google.
To install the plugin, extract the files from the archive to a directory named "geosack" in your vendors/plugin directory.
Using the plugin is straightforward:
To use the geocoding service you will need a google API key, obtained from here:
http://code.google.com/apis/maps/signup.html
Once you have this key, you can create your Geosack object:
geosack = Geosack::Base.new(:api_key => YOUR_API_KEY)
Then you are free to start making geocode requests.
result, location = geosack.geo(:q => 'San Jose')
The placename to be looked is specified by the :q parameter. The only other parameter is :output which is the response format - KML/XML/JSON. The default response format is XML.
You don't need to worry about the 2 second delay that google requires - geosack takes care of that for you.
The result object is a reflection of the response returned by google to your request. Anything other than a 200 (G_GEO_SUCCESS) is a failure.
You will also need to check for 610 (G_GEO_TOO_MANY_QUERIES) responses which indicate you have reached your limit for the 24 period. Note that this limit only applies to new requests sent to google - if you send a request that has already been cached by geosack, the response will come from geosack, not from google, and your daily limit will be unaffected.
Heres an example to get your started:
geosack = Geosack::Base.new(:key => API_KEY)
result, resp = geosack.geo(:q => lookup)
if result == Geosack::StatusCodes::G_GEO_SUCCESS
doc = REXML::Document.new(resp)
doc.root.each_element('//Response') do |response|
response.each_element('//Placemark') do |place|
longitude,latitude = place.elements['//coordinates'].text.split(',')
end
end
else
# failed
end
Have fun, and feel free to contact us at mailto:info@packetnode.com with any questions or suggestions.