Ruby Geolocation Gem/Plugins
Your current best bet is probably GeoKit (http://github.com/andre/geokit-gem for gem, http://github.com/andre/geokit-rails for plugin). It has built in functionality for Yahoo and Google API keys, distance calculation helpers, reverse geolocation, etc.
However, GeoMereLaal (http://github.com/parolkar/geo_mere_laal/) is based on the working draft of the W3C Geolocaton API. It's very limited at the moment as I could only get it to work in Firefox, but it will be more accurate as it's supported by more browsers since it uses more methods of Geolocation (GPS, WiFi, cookies, IP).
How to find where gem files are installed
Use gem environment
to find out about your gem environment:
RubyGems Environment:
- RUBYGEMS VERSION: 2.1.5
- RUBY VERSION: 2.0.0 (2013-06-27 patchlevel 247) [x86_64-darwin12.4.0]
- INSTALLATION DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0
- RUBY EXECUTABLE: /Users/ttm/.rbenv/versions/2.0.0-p247/bin/ruby
- EXECUTABLE DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/bin
- SPEC CACHE DIRECTORY: /Users/ttm/.gem/specs
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-12
- GEM PATHS:
- /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0
- /Users/ttm/.gem/ruby/2.0.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/ttm/.rbenv/versions/2.0.0-p247/bin
- /Users/ttm/.rbenv/libexec
- /Users/ttm/.rbenv/plugins/ruby-build/bin
- /Users/ttm/perl5/perlbrew/bin
- /Users/ttm/perl5/perlbrew/perls/perl-5.18.1/bin
- /Users/ttm/.pyenv/shims
- /Users/ttm/.pyenv/bin
- /Users/ttm/.rbenv/shims
- /Users/ttm/.rbenv/bin
- /Users/ttm/bin
- /usr/local/mysql-5.6.12-osx10.7-x86_64/bin
- /Users/ttm/libsmi/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /usr/local/bin
Notice the two sections for:
INSTALLATION DIRECTORY
GEM PATHS
Ruby on rails gem for google map integration
I've used the YM4R, Georuby and spatial adapter gems with good results before.
see this description.
Getting a user country name from originating IP address with Ruby on Rails
You can use geoip
gem.
environment.rb
config.gem 'geoip'
Download GeoIP.dat.gz
from http://www.maxmind.com/app/geolitecountry
. unzip the file. The below assumes under #{RAILS_ROOT}/db
dir.
@geoip ||= GeoIP.new("#{RAILS_ROOT}/db/GeoIP.dat")
remote_ip = request.remote_ip
if remote_ip != "127.0.0.1" #todo: check for other local addresses or set default value
location_location = @geoip.country(remote_ip)
if location_location != nil
@model.country = location_location[2]
end
end
Search by location using an array with geokit gem
You can chain multiple calls like:
model = Model.in_range("0".."5",:origin => [Lat1, Lng1]).in_range("0".."5",:origin => [Lat2, Lng2])
For deep reference, check the method at https://github.com/geokit/geokit-rails/blob/3a7b4e1083dd06d486307ebf71c36608186c8d73/lib/geokit-rails/acts_as_mappable.rb#L134
How do you get a ruby gem into the include path for require
It's actually not to hard to do this manually. Let's say you have a library whatever.rb
that you want to distribute as a gem.
- make a directory
lib
and put a copy ofwhatever.rb
inlib/whatever.rb
. - make a file
whatever.gemspec
, and put the following in there, filling in the appropriate values:
Gem::Specification.new do |spec|
spec.name = 'the-name-of-your-gem'
spec.version ='0.0.1'
# this is important - it specifies which files to include in the gem.
spec.files = ["lib/whatever.rb"]
# optional, but useful to your users
spec.summary = "A more longwinded description of your gem"
spec.author = 'Your Name'
spec.email = 'you@yourdomain.com'
spec.homepage = 'http://www.yourpage.com'
# you did document with RDoc, right?
spec.has_rdoc = true
# if you have a ruby forge project
spec.rubyforge_project = 'your-project-name-on-rubyforge'
# if you have any dependencies on other gems, list them thusly
spec.add_dependency('hpricot')
spec.add_dependency('log4r', '>= 1.0.5')
end - now, to build the gem, use the
gem build
command:
% gem build whatever.gemspec
Successfully built RubyGem
Name: the-name-of-your-gem
Version: 0.0.1
File: the-name-of-your-gem-0.0.1.gem
% - You can test locally by using
gem install the-name-of-your-gem-0.0.1.gem
To use your library in a script then, simply do the following at the top:
require 'rubygems' # puts gem libraries in the require path
require 'whatever' # loads your library
For more on what the various settings in the gemspec
file, check the GemSpec Reference.
Personally, I use rubygems a lot to package executable scripts as well, and find it very handy for that.
Create and store the geo-location data
I would reccomend PostGIS and then use a Ruby Connector.
Rails 3.2.3 GeoLocation Using MaxMind
Did you try https://github.com/imajes/geokit-gem?
How to store and find records based on location?
It should be easy to find the math to solve that, providing lat/long coordinates.
Or you could use some full featured gem to do that for you like Geocoder, that supports Mongoid or MongoMapper.
Next time you need some feature that might be a commun world problem, first check if there is a gem for that at ruby-toolbox, for this case here are some other gems for geocoding
Related Topics
Open_Id_Authentication - "Openidauthentication.Store Is Nil. Using In-Memory Store." Problem
Error Installing Debugger-Linecache in Ruby 1.9.3
Rake VS. Thor for Automation Scripts
How to Convert a JSON Formatted Key Value Pair to Ruby Hash with Symbol as Key
Convert String with Hex Ascii Codes to Characters
Puppet/Facter "Could Not Retrieve Fact Fqdn": How to Fix or Circumvent
Check If User Is Active Before Allowing User to Sign in with Devise (Rails)
After Gem Update: Test Fail with "Asset Was Not Declared to Be Precompiled in Production"
Converting Upper-Case String into Title-Case Using Ruby
Ruby Float#Round Method Behaves Incorrectly with Round(2)
How to Handle Constants in Ruby When Using Rails
Installed Rails But the Rails Command Says It's Not Installed
Rspec: How to Test File Operations and File Content