how to use extensions #2195
-
Hi, im new to this, currently im developing my apps using react. currently im able to fetch my data according to the documentation like this I have user and business table, both have latitutde and longitude column, what im trying to do is to fetch nearest business sort by distance to the user using the earthdistance extensions on the supabase. can anyone show how to implement that, tq |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @atiframlee, Here's an example using earthdistance's <@> operator: -- create this function on the db
create or replace function distance(long1 float8, lat1 float8, long2 float8, lat2 float8) returns float8 as $$
select POINT(long1, lat1) <@> POINT(long2, lat2) as distance;
$$ language sql;
-- suit this to use your cities table // call it through supabase rpc: https://supabase.io/docs/reference/javascript/rpc
const { data, error } = await supabase
.rpc('distance', {long1: -81.3927381, lat1: 30.2918842, long2: -87.6473133, lat2: 41.8853881})
console.log(data);
873.203527399339 In general, you can expose any SQL functionality with a function and call it through RPC. |
Beta Was this translation helpful? Give feedback.
Hey @atiframlee,
Here's an example using earthdistance's <@> operator:
In general, you can expose any SQL functionality with a function and call it through RPC.