Ideas for search by distance
February 22, 2008 6:17 AM   Subscribe

How can I best display a search by distance feature on a website?

I am building a website which will have a search by distance feature eg find a cafe within 5 miles of x. It's for the UK and uses postcode/latitude longitude info. The obvious way to display it is to have users select a postcode, and a distance radius. However, this means the user has to know the starting postcode, which they may not (if you are searching in different city from your own perhaps). So I could display a drop down of placenames associated with postcodes. But at its best this gives me a drop down with hundreds of options, and in large cities there are multiple postcodes which confuses it even more. I've experimented with a series of selects, ie first select a region, then from places within that region, which is a bit better, but not yet quite what I want. Whether this is because of the data I have, or the way of displaying it I am not sure.
So I am looking for ideas and suggestions, examples even, of good ways to display this feature that will work well for the user. I'm going round in circles with it a bit, and need some fresh input.
posted by zingzangzung to Computers & Internet (9 answers total) 2 users marked this as a favorite
Check out Google Suggest. I'm sure you can figure out the rest from there.
posted by Leon at 7:00 AM on February 22, 2008

I did something similar for a location finder in Illinois. I made a field for the user to type their ZIP code, and a map of counties they could click. The map was originally a list of a hundred items, but as you've found, that's pretty annoying.

If they input a ZIP code, it's geocoded for lat/long and the database shows locations within 15 miles. If they choose a county, it shows all the locations in that county.

I'm sure there'll be better answers to come.
posted by Plug Dub In at 7:02 AM on February 22, 2008

If they don't know their postcode then don't use postcode in the selection.

You see my point? You're confusing the issue for them by doing this drop-down with postcodes even after you've established that they don't know what it is - if they did, they'd be using it.

If you have a list of regions for them to select from then you might want to simply ask them to type in their region. The quickest - and most existing re-usable code existant - option I can think of would be to turn that typed-into stuff into soundex to try to find a match.

If that doesn't work then drill down graphically with simple maps. Start out with a zoomed-out depiction with, say, 4 regions. As they pick a region you zoom and show the sub-regions. At the bottom level you can draw by post code - they don't have to know what that is, but since your other searches seem to use that as a basis you may as well be consistent.

If you want to be really flash you could keep track of what they put in and match it with what they select, so as to have that match in the future.
posted by phearlez at 7:11 AM on February 22, 2008

How precise does the pinpointing of the location need to be? I've done branch locator type things where it was fine to have a single static image map of the UK to supplement the postcode input (which usually allowed towns too), but the branches were fairly widely spaced.
I assume your results page shows items on a map? Could you allow users to refine their location via that map?

(By the way, are you sure letting the user specify a radius is helpful/necessary? Usually it's better to look outwards until you find a sensible number of items, otherwise users will tend to end up with too many or too few results)
posted by malevolent at 7:19 AM on February 22, 2008

Response by poster: Hi All. The search has to be precise to the level of UK postcode outcodes , which is nearly 3000 distinct placenames (plus of course every large town or city has loads, Glasgow for example has 29 different outcodes). Visual search via maps is good, but I think would need to zoom in to a fairly detailed map, for the Glasgow example, 29 separate regions on one map? I'd probably need to zoom in further. This then becomes a problem of acquiring all the required maps.
Phearlez, I can offer users either the postcode, or its associated placename, it's still a ton of data.
The implementation I have been trying is similar to this one. I wondered if anyone knew of other non visual approaches that were user friendly.
Good point about the radius Malevolent, I'll have to think how that will work in this application.
If it matters, the items being searched aren't static (like store branches) in that it's more like a search for all houses for rent within 5 miles of London EC1 for example, which will change constantly, so I can't have fixed locations pinpointed on maps.
posted by zingzangzung at 8:03 AM on February 22, 2008

So you have a database of cafes that includes either the address or (ideally) the lat/long of each cafe, right? If you don't have lat/longs, then your distance search becomes more difficult. Use a free geocoding service like Google to store the latitudes and longitudes of all the cafes.

Next, have the user input their address. Geocode it to get the lat/long of the address.

Then, grab your cafe coordinates from your database and compute the distance between each one and the user's address. To compute the distance between two coordinates is fairly easy and well-documented on the Internet, just search for "great circle distance computation" and you'll get the algorithm.

This part gets tricky because performance becomes a concern if your cafe result set is too big. So limit it by the distance the user wants to search within.

Sort your results and spit them back to the user.
posted by drinkcoffee at 8:27 AM on February 22, 2008

drinkcoffee has a correct answer: this is how myself and a coworker wrote a simple radius search for a job map mashup this past year. Once you get everything geocoded, it's pretty simple to calculate radius. The folks at google have already taken care of reducing incomplete addresses to a geocode (like how you'll at least get a spot in oakland if you type in "oakland,ca" and nothing else into google maps), so take advantage of it! Cache geocodes aggressively because it will speed up your mapping application.
posted by fishfucker at 9:01 AM on February 22, 2008

Best answer: There isn't really going to be a viable non-visual way to get below the town level of detail, and once you get to that point you start needing decent map images for a visual search, so unless you hook into something like Google Maps (which could handle both mapping and geocoding) it's going to be tricky.

If you want a standalone solution, the best compromise I can think of is to allow postcode, town or clicking a UK map. Providing the map is projected for lat/long (rather than UK grid, which is what I worked with using OS maps) you can easily convert the image map x/y to give you a rough location to work with. You could then offer a second level of simple clickable map for major cities to arrive at more precise coords for those.
You might want to consider upgrading to sector level postcode->lat/long data; it's not much bigger but will make proximity searches a lot more effective (and would be precise enough to let you plot the results on the low-detail maps the user has clicked on).
posted by malevolent at 9:08 AM on February 22, 2008

The free GeoNames database may be helpful if you have to implement your own geolocation. Also, be aware of the spatial extensions in newer versions of MySQL (if that's how you roll).
posted by mrflip at 12:06 AM on April 2, 2008

« Older Getting music out of my brain away from home.   |   Redoing Bathroom Without Taking a Bath Newer »
This thread is closed to new comments.