Geographical location in Ruby / Sinatra?

I am creating a small application in Sinatra, and I would like to determine the cities of my users from their zip code (which they will enter), the distance between them and other users (by zip code) and possibly a heat map of zippers.

How can I do it? I tried the geoip gem, but it doesn't seem to do what I want. Do I use an external service like Google Maps (obviously I need this for a heat map)?

Thanks for any help.

+7
ruby geolocation sinatra
source share
1 answer

GeoKit gem sounds like the right fit for what you would like to do.

It abstracts interfaces with various geocoding services (Yahoo, Google, etc.) and provides code for calculating distances.

You can geocode zip files to obtain locations, access address-based location information, and calculate distances between your locations.

Here's a quick start, shameless, copied from the linked page to give you an idea of ​​how the library works:

irb> require 'rubygems' irb> require 'geokit' irb> a=Geokit::Geocoders::YahooGeocoder.geocode '140 Market St, San Francisco, CA' irb> a.ll => 37.79363,-122.396116 irb> b=Geokit::Geocoders::YahooGeocoder.geocode '789 Geary St, San Francisco, CA' irb> b.ll => 37.786217,-122.41619 irb> a.distance_to(b) => 1.21120007413626 irb> a.heading_to(b) => 244.959832435678 irb(main):006:0> c=a.midpoint_to(b) # what halfway from a to b? irb> c.ll => "37.7899239257175,-122.406153503469" irb(main):008:0> d=c.endpoint(90,10) # what 10 miles to the east of c? irb> d.ll => "37.7897825005142,-122.223214776155" 
+9
source share

All Articles