# Doing a radius/location search

**URL:** <https://www.ruby-forum.com/t/doing-a-radius-location-search/134991>\
**Category:** Rails\
**Created:** [April 16, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/doing-a-radius-location-search/134991 "2008-04-16T21:44:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [April 16, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/doing-a-radius-location-search/134991/1 "2008-04-16T21:44:37Z")

</div>

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.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [April 16, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/doing-a-radius-location-search/134991/2 "2008-04-16T22:24:44Z")

</div>

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

Harry

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [April 17, 2008, 2:10am UTC](https://www.ruby-forum.com/t/doing-a-radius-location-search/134991/3 "2008-04-17T02:10:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [April 18, 2008, 12:58am UTC](https://www.ruby-forum.com/t/doing-a-radius-location-search/134991/4 "2008-04-18T00:58:36Z")

</div>

> I’ve also used the zip code plugin at this location:[http://zipcodesearch.rubyforge.org/](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://agilewebdevelopment.com/plugins/actsaslocateable)  
[http://www.baconbear.com/articles/2006/12/29/actsaslocateable-released](http://www.baconbear.com/articles/2006/12/29/actsaslocateable-released)

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [April 17, 2008, 5:33am UTC](https://www.ruby-forum.com/t/doing-a-radius-location-search/134991/5 "2008-04-17T05:33:31Z")

</div>

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](http://maps.google.com) and get a valid  
response.

[http://geokit.rubyforge.org/](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
