Fastest Way to Find Distance Between Two Lat/Long Points

后端 未结 15 1011
时光说笑
时光说笑 2020-11-21 10:12

I currently have just under a million locations in a mysql database all with longitude and latitude information.

I am trying to find the distance between one point a

15条回答
  •  Happy的楠姐
    2020-11-21 10:46

    Using mysql

    SET @orig_lon = 1.027125;
    SET @dest_lon = 1.027125;
    
    SET @orig_lat = 2.398441;
    SET @dest_lat = 2.398441;
    
    SET @kmormiles = 6371;-- for distance in miles set to : 3956
    
    SELECT @kmormiles * ACOS(LEAST(COS(RADIANS(@orig_lat)) * 
     COS(RADIANS(@dest_lat)) * COS(RADIANS(@orig_lon - @dest_lon)) + 
     SIN(RADIANS(@orig_lat)) * SIN(RADIANS(@dest_lat)),1.0)) as distance;
    

    See: https://andrew.hedges.name/experiments/haversine/

    See: https://stackoverflow.com/a/24372831/5155484

    See: http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/

    NOTE: LEAST is used to avoid null values as a comment suggested on https://stackoverflow.com/a/24372831/5155484

提交回复
热议问题