Jump to content

How to Perform Location Based Searches


unemployment

Recommended Posts

This is more of a logic question.  I have built a beta version of my site and I am currently working on getting beta V2 out, but I want to implement location based searching of users (particularly with a google api integration).  Aside from the API stuff, how do I perform location based searches on the backend.  For instance, I have all of my users locations (city, state, country), but how can I say to grab all users by that state when that state is typed in or grab all users by that city when the city is typed in and the same thing for country?  Is there anyway to set this up with a default radius too?  So if I search for a town, I get a 20 Mile radius result?  I really have no idea how to go about the dynamic look up match. Any good tutorials on this?

Link to comment
Share on other sites

Getting all users by a state or city is very easy. Assuming you have this data in a database then just query with " WHERE city='whatever' " or the same for state/country.

 

Distance based calculations are a bit more complicated, though. First you'll need to get the latitude and longitude for each city. The Google Maps API could probably help you here. They have services where you can pass addresses in and get the latitude/longitude returned.

 

Once you have that you can calculate the distance between two lat/long points with a bit of math. See http://www.movable-type.co.uk/scripts/latlong.html for details on how to do that (implementation is in JavaScript but it shouldn't be too hard to convert to PHP).

 

Assuming you don't have thousands and thousands of users you could probably just loop through each user and calculate their distance from the city in question and kick them out of the result set if they are too far away

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.