views:

47

answers:

1

I'm building a web application that requires users to provide their location (similar to the way Facebook does it). I'm not sure on the best way to implement this. I require the ability to search through records of another entity (call it 'events') which also have a physical location attached to it, and match the users in the same or close enough location. I only require resolution to individual cities/towns.

The way I allow the user to provide his/her location (including validation and how free-form I would allow this input), the way to store this field in the DB, etc. obviously depend on each other. My current thinking is to allow free-form entry, store as a text string, and match by breaking up the location string into an array (delimited by commas and period characters), then comparing the user location array to the event location array.

Is there any way that I can make the problem easier on myself by validating against a list of locations provided by a web service somewhere? How is this problems usually handled?

Any help would be greatly appreciated!

+2  A: 

I think your approach can be acceptable depending on the amount of accuracy you require.

A more accurate (and probably more complex) approach would be to actually store all locations in coordinates. You can use a geolocation webservice (I believe Google provides one, others too) to map location names into coordinates. You can also use IP based geolocation to try to guess where the user is so they won't necessarily have to type their own address.

There are formulas (and probably libraries) for calculating distances between coordinates relative to each other, which would make it easy to display things within a certain radius from the user's coordinates and stuff like that.

You could also consider using the new HTML5 geolocation API which is supported by the latest browsers

Jani Hartikainen
+1 for Geolocation API
Gordon
I'm not sure how the geolocation API can help me - if the user provides an invalid location that doesn't validate with the API, then a distance measure between the user and the event's location (which will be much better specified with a street address/Google location API 'place') is not possible.
ubermensch
You can use the geo API to detect the user's location from devices such as the iPhone without them having to manually input (or know) their exact location. Of course it may or may not be useful to you depending on the sort of application you're working on.
Jani Hartikainen
Ah, I see. Thanks Jani. The only problem with that, I guess, is that a user will have to manually allow me to collect user agent location information - not particularly user friendly and a lot of users would be suspicious of such functionality.
ubermensch