Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Location from IP, geocoding, distance calculation, and geo/map utilities
Ruby

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.document
.gitignore
.rspec
Gemfile
Gemfile.lock
LICENSE.txt
README.rdoc
Rakefile
VERSION
geo_magic.gemspec

README.rdoc

Geo Magic

This little clever project was the result of me not being satisfied with existing Geo Location and Coding solutions out there to suit my needs. Many of the projects did not return quality data (fx when requesting IP or location) and others were tightly bound to Active Record and SQL databases for calculations etc.

This little gem should get you going…

  • Get IP and location data using freegeoip.net

  • Calculate of distance between map points using haversine (supports multiple distance units)

You can either include the complete library like this:

require 'geo_magic'

Or require only the functionality you need (see usage examples below)

IP and location data

Uses freegeoip.net JSON API

RSpec 2 examples of API usage

require 'geo_magic/remote'

it “should get the remote IP address” do

ip = GeoMagic::Remote.my_ip    
puts "ip: #{ip.inspect}"

end

it “should get other location” do

location = GeoMagic::Remote.location_of '74.200.247.59'
puts "location: #{location['city']}"

end

it “should get my location” do

location = GeoMagic::Remote.my_location
puts location
puts "location: #{location['city']}"

end

Distance calculation

Uses haversine formula

RSpec 2 examples of API usage

require 'geo_magic/calculate'

it “calculates distance using array args” do

dist = GeoMagic::Calculate.distance [@long1, @lat1], [@long2, @lat2]    
puts dist

end

it “calculates distance using Point args” do

from_point = GeoMagic::Point.new @long1, @lat1    
to_point = GeoMagic::Point.new @long2, @lat2

puts "from: #{from_point}, to: #{to_point}"

dist = GeoMagic::Calculate.distance from_point, to_point
puts dist

end

it “calculates distance using Hash args (short)” do

from_point = GeoMagic::Point.new(@long1, @lat1).to_hash :short
to_point = GeoMagic::Point.new(@long2, @lat2).to_hash :short

puts "from: #{from_point}, to: #{to_point}"

dist = GeoMagic::Calculate.distance from_point, to_point
puts dist

end

Meta magic

You can also include the functionality directly into any class like this

class Map

include GeoMagic::Calculate

end

class MapCalc

geo_magic :calc

end

it “calculates distance using array args” do

dist = Map.distance [@long1, @lat1], [@long2, @lat2]    
puts dist

end

it “calculates distance using array args” do

dist = MapCalc.distance [@long1, @lat1], [@long2, @lat2]    
puts dist

end

Contributing to geo_magic

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright © 2011 Kristian Mandrup. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.