https://supabase.com/ logo
#flutter
Title
# flutter
a

Alvin

08/18/2021, 12:22 PM
Hello everyone 👋 Is there a way to calculate distance between coordinates just like firebase Geofire in supabase?
k

Kosh

08/18/2021, 12:24 PM
You will need to create an RPC function for this. and fyi: you can't really say like firebase, because geolocation in firebase is just trash and costy.
anyway, lets say u have a geo location stored in ur database u can then easily do this
Copy code
ST_Distance(myTable.savedLocation, ST_GeogFromText(my_location))
this will return u a distance between two locations, u can also use it to order by ur query 🙂
a simple function can be
Copy code
$$;
CREATE
OR REPLACE FUNCTION public.select_nearby_feed(my_location text) RETURNS SETOF feed LANGUAGE sql AS $$ 
   select * from feed 
    ORDER BY ST_Distance(feed.location, ST_GeogFromText(my_location)), created_at DESC;
$$;
and from flutter.
Copy code
_supabase.client.rpc('select_nearby_feed', params: {'my_location', 'POINT(longitude latitude)'})
.execute();
look into postgis
b

bdlukaa

08/18/2021, 12:56 PM
or you can wait til supabase functions are out
b

burggraf

08/28/2021, 5:41 PM
Did you get this resolved? I’m doing this using a PostgreSQL function and can share it with you if you need it.