Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for user physical location (input, data type, display and validation)

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!

like image 936
ubermensch Avatar asked Nov 06 '22 11:11

ubermensch


1 Answers

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

like image 58
Jani Hartikainen Avatar answered Nov 11 '22 02:11

Jani Hartikainen