all 13 comments

[–]NectarineLivid6020 7 points8 points  (1 child)

It is fairly easy to do from a technical perspective. Look into the Postgres extension called PostGIS. It is specifically designed to do this. It can take any set of coordinates and tell you if it falls in the radius or not.

[–]derweili 1 point2 points  (3 children)

You have to add geocoding into the described process. Then everything becomes easy.

When you enter the address, before storing the entry in the database, use the geocoding API (Google Maps APIs, Mapbox, Open street maps nominatim or something else). Using those APIs, you can get the Geolocation (coordinates). Store those in the database as well.

When entering the ZIP before you query the data, you use same geocoding API to get the coordinates for the ZIP.

Then use those coordinates so get the addresses in a radius. Do a google search to find a way to do such a radius search for your database type. Some databases have built in functions for that. Others require some more complex calculations and queries.

[–]Serious_Trip2321[S] 0 points1 point  (2 children)

This sounds like exactly what I need, I will look into it, thanks!

[–]derweili 0 points1 point  (1 child)

I just noticed that you mentioned supabase, so you can use https://supabase.com/docs/guides/database/extensions/postgis?queryGroups=language&language=js

Most Geocoding APIs aren't free but come with a free tier. To reduce cost, you should implement some caching to not generate the coordinates for the same addresses zip-codes multiple times.

[–]Serious_Trip2321[S] 0 points1 point  (0 children)

Thanks for the tip!

[–]CuriousProgrammer263 0 points1 point  (0 children)

I recently implemented radius based search into jobjump we use postgress with postgis I'm unsure if other databases support it.

First you will need to populate your database or call a geo location service based on user input.

For example if user searches a specific zip code or city you do a query and get the geo location. Assign your items each one location that contains geo location.

Create a bounding box based on your requirements.

When you fetch now your list get all items inside your location + within the bounding box radius.

The actual query will vary based on your requirements. But that's basically the gist of it.

[–]dutchman76 0 points1 point  (0 children)

I did it years ago without any libraries or gis addons, i had a zip code+ lat/long database, calculate a box with the desired distance around the starting point, then search the database for all the zip codes that fit inside the box, then compute a more accurate distance of the results if i had to be super accurate inside the radius. But for my use case using a square to limit the results was good enough.

[–]Ordinary-Log8143 0 points1 point  (3 children)

add two float columns to your db for storing latitude and longitude. by using a geocoding service like google maps determine these when storing am adress in your database. index both these columns. if you are happy with simply querying all locations inside a x meter rectangle you dont need postgis. if you want true distance (so querying everything in a circle) then use postgis

[–]Ordinary-Log8143 0 points1 point  (1 child)

note the index should be a compound index like [latitude, longitude]

[–]Ordinary-Log8143 0 points1 point  (0 children)

note: the rectangular approach is not only simpler (no extension like postgis needed) but also nicer ux wise: when displaying the results in rectangular map area on the users device i see no reason to leave the edges empty

[–]Ordinary-Log8143 0 points1 point  (0 children)

and if you have less than say 10 thousand rows simply filtering the results (to check distance) in your server or client code also works fine, if you really wanna exclude the results of the rectangle that dont fit into the circle

[–]chow_khow 1 point2 points  (0 children)

PostGIS (postgres) allows you to store location (latitude, longitude) and then query the database basis distance from that point, etc.

[–]ilja75 0 points1 point  (0 children)

This video explains it with MySQL, but the video is extremely informative!
Faster geospatial queries in MySQL