Skip to content

Instantly share code, notes, and snippets.

@dorman99
Created October 20, 2021 17:39
Show Gist options
  • Save dorman99/b496cfca428d390c6f975b26c3cd5c61 to your computer and use it in GitHub Desktop.
Save dorman99/b496cfca428d390c6f975b26c3cd5c61 to your computer and use it in GitHub Desktop.
method to generate amount of distance between 2 position on km
CREATE OR REPLACE FUNCTION distance(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT) RETURNS FLOAT AS $$
DECLARE
x float = 111.12 * (lat2 - lat1);
y float = 111.12 * (lon2 - lon1) * cos(lat1 /92.215);
BEGIN
RETURN sqrt(x * x + y * y);
END
$$ LANGUAGE plpgsql; // km
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment