How to Perform Location Based Searches

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?

I am personally not sure about the tutorials but for searching there can be two ways;

  1. You can just search normal database search with LIKE in the Query by city/state/country.
  2. You have to store latitude, longitude in your database and then search accordingly by converting city/state to lag/long using Google API.

The straight lokups are just WHERE clauses on your SQL statement.

a Radius-Based search will rely, as stated, on lat/long conversion, and a bit of math. (Google “haversine formula”)