In my case, I used the geolocation of browsers and tried to find the nearest city / state based on the coordinates that I had in the table.
table structure:
id zipcode city_state lat lon 1 12345 Example, GA 85.3 -83.2
I recommend that you thoroughly test this before use - maybe some settings are needed, but I came up with this for a start
SELECT city_state, zipcode, ( Abs( lat - -33.867886 ) + Abs( lon - -63.987) ) AS distance FROM zipcodes ORDER BY distance LIMIT 1;
For Laravel users:
$city = Zipcodes::selectRaw ('city_state, zipcode, ( ABS( lat - ? ) + ABS( lon - ?) ) AS distance', [$lat, $lon]) ->orderBy('distance') ->first(); echo $city->city_state
Hope this helps someone someday.
source share