开发者

Geo Location in Ruby/Sinatra?

I'm creating a small app in Sinatra, and I'd like to determine my users' cities from their zip code (which they would input), the distance between them and other users' (by zip code), and possibly a heat map of the zips.

How would I do this? I've tried the geoip gem, but it doesn't seem to do what I want. W开发者_C百科ould I use an external service like Google Maps (obviously I'd need this for a heat map)?

Thanks for any help.


The GeoKit gem sounds like a good fit for what you'd like to do.

It abstracts away the interfaces to various geocoding services (Yahoo, Google, etc) and provides code for distance calculations.

You can geocode the zips to get locations, access address information about the location, and calculate the distances between your locations.

Here's the quick start, shameless copied from the linked page, just 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's halfway from a to b?
irb> c.ll
=> "37.7899239257175,-122.406153503469"
irb(main):008:0> d=c.endpoint(90,10)     # what's 10 miles to the east of c?
irb> d.ll
=> "37.7897825005142,-122.223214776155"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜