PostgreSQL latitude longitude query

后端 未结 5 740
故里飘歌
故里飘歌 2020-12-13 02:31

i have latitude and longitude columns in location table in PostgreSQL database, and I am trying to execute distance query with a Postg

5条回答
  •  春和景丽
    2020-12-13 02:59

    This module is optional and is not installed in the default PostgreSQL instalatlion. You must install it from the contrib directory.

    You can use the following function to calculate the approximate distance between coordinates (in miles):

     CREATE OR REPLACE FUNCTION distance(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT) RETURNS FLOAT AS $$
    DECLARE                                                   
        x float = 69.1 * (lat2 - lat1);                           
        y float = 69.1 * (lon2 - lon1) * cos(lat1 / 57.3);        
    BEGIN                                                     
        RETURN sqrt(x * x + y * y);                               
    END  
    $$ LANGUAGE plpgsql;
    

提交回复
热议问题