Doing a radius/location search

The application I’m developing allows people to create events in their
local
area; as part of this feature I want to display a list of, say, the five
most recent events within 30 miles of a person’s location.

I have a User model and a Profiles model which contains the user’s
location
(city, state, zip code) among other non-related personal information.
So
what I would like to do is be able to do is to get a list of upcoming
events
relative to that person’s location, and display it on their “control
panel”
when they log in to the site.

Also, at some point in the future I want to integrate with Google Maps
to
display a map of the Event’s location on a particular event’s detail
page
(i.e. “show” action in the controller); this isn’t required for the Beta
version of the site, but it’s something to consider since it will be a
feature later.

I’ve been looking at the Google Maps API but I’m not sure if it would
let me
do what I really need, which is to query for all zip codes within 30
miles
of a given zip code, so I can display the events to users who live
within
that radius and hopefully get them to attend.

I have the book “RailsSpace”, and it implements something similar to
what I
want with a database, but if I go this route will it prevent me from
using
Google Maps at a later date? I haven’t really looked yet to see if
there
are any plugins that can help me out with this - I want to get a handle
on
the concepts I need, and then work on the implementation.

To summarize, what I want to be able to do is:

  • Display a list of the five most recent events within 30 miles of a
    user’s
    location on their profile page
  • Send an email to all users within 30 miles of an event upon creation,
    notifying them that a new event has been scheduled in their area (this
    one I
    can probably figure out myself once I have the location part done)
  • Enable a user to search for upcoming events within a specified radius
    of a
    zip code
  • At some point in the future, integrate with Google Maps to display a
    map
    of the event in question; presumably this would be based on the event’s
    zip
    code

Anyone have any pointers on how to accomplish this? My main point of
concern is I’m not sure if I should use the approach of the RailsSpace
book,
which as I recall is to set up a database containing the location data,
and
do some funky math to get the radius, or if there’s some third-party API
available that I could mash-up with and leverage, especially if it would
make future integration easier.

Any help would be appreciated.


Wayne M.
[email protected]

Check out the Geokit plugin (for distance finders) and the YM4R-GM
plugin for Google map display.

Harry

I’ve also used the zip code plugin at this location:
http://zipcodesearch.rubyforge.org/
with good results.

I’ve also used the zip code plugin at this location:http://zipcodesearch.rubyforge.org/
with good results.

The acts_as_locateable plugin is based on zipcodesearch with the same
features plus some improvements.

http://agilewebdevelopment.com/plugins/actsaslocateable
http://www.baconbear.com/articles/2006/12/29/actsaslocateable-released

I second Harry’s suggestion for Geokit. It’s super easy and very
powerful. Let’s you do a simple query like:

Event.find(:all, :origin=> @user.address, :within=>30, :limit =>
5, :order => ‘distance asc’)

That would find the 5 events within 30 miles ordered by closest to the
user’s address, which could be any “geolocatable representation”…
i.e. anything you’d type in at maps.google.com and get a valid
response.

http://geokit.rubyforge.org/

Oh, and YM4R-GM is good for getting started, but if you start to get
fancy with Google Maps, you may find, as I did, that it’s best to just
build it directly with Javascript using the Google Maps API. It seemed
weird to me to be building view logic within the controller, so I
ended up ditching YM4R and doing the javascript directly via the view
and helpers.

Good luck!

-Danimal