geo querys and sorting by distance #5390
-
Hi, Thanks for any help. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 25 replies
-
If your data is available as longitude and latitude (floats), then you could make use of haversine formula. |
Beta Was this translation helpful? Give feedback.
-
You can also use the postgres PostGIS extension with a point data type and then make queries (using the rpc functions mentioned above) for simple to complex retrieval of data. It is reasonably complicated to get things going though, and you can't use the table UI to add the column. But should be fast as built in and let you do more advanced things like find in a shape versus just distance if ever needed. |
Beta Was this translation helpful? Give feedback.
-
I found this project https://www.madewithsupabase.com/p/supabase-geodb, maybe that's helpful |
Beta Was this translation helpful? Give feedback.
-
Yes, Postgres supports geo queries much better than Firestore! In order to query data and order it by distance, you would first need to enable postgis extension in your Supabase project. Your table could look something like this: create table if not exists public.videos (
id uuid not null primary key DEFAULT uuid_generate_v4 (),
created_at timestamp with time zone default timezone('utc' :: text, now()) not null,
description varchar(320) not null,
location geography(POINT) not null
); The key here is the Then you can create a Postgres function like this: create or replace function test_nearby(lat float, long float)
returns setof videos
as $$
select *
from videos
order by location <-> st_point(long, lat)::geography;
$$
language sql; The above function can be called like this: { data, error } = await supabase.rpc('nearby_videos',
{location: 'POINT(${longitude} ${latitude})'}
) I actually utilize a lot of these geo queries in my open source project Spot, so it might help out to check it out. |
Beta Was this translation helpful? Give feedback.
-
I don't really know how, but I want to build something like uber where the driver's location will be inserted to the live_location table so that the users can fetch that when they want to request for a ride. I have been able to insert the driver's location to the table, but how can I make the table realtime such that it reads the changes of the driver's location and provide the new latlng. Like when the driver moves, I need the table to be updated with the new latlng. I am new to supabase and I need help with that. I dont want to be adding new data row to my live_location table, i want to update a certain row based on the ID of that driver such that a driver will only have one row of data which will be inserted once and then be updating itself once the location changes. And when the driver goes offline, I want to delete that record from the live_location table. |
Beta Was this translation helpful? Give feedback.
Yes, Postgres supports geo queries much better than Firestore!
In order to query data and order it by distance, you would first need to enable postgis extension in your Supabase project.
Your table could look something like this:
The key here is the
location
column withgeography(POINT)
type which represents a geographic location on earth.Then you can create a Postgres function like this: