Calculate distance between 2 GPS coordinates

前端 未结 29 3529
青春惊慌失措
青春惊慌失措 2020-11-21 23:34

How do I calculate distance between two GPS coordinates (using latitude and longitude)?

29条回答
  •  逝去的感伤
    2020-11-22 00:05

    This is version from "Henry Vilinskiy" adapted for MySQL and Kilometers:

    CREATE FUNCTION `CalculateDistanceInKm`(
      fromLatitude float,
      fromLongitude float,
      toLatitude float, 
      toLongitude float
    ) RETURNS float
    BEGIN
      declare distance float;
    
      select 
        6367 * ACOS(
                round(
                  COS(RADIANS(90-fromLatitude)) *
                    COS(RADIANS(90-toLatitude)) +
                    SIN(RADIANS(90-fromLatitude)) *
                    SIN(RADIANS(90-toLatitude)) *
                    COS(RADIANS(fromLongitude-toLongitude))
                  ,15)
                )
        into distance;
    
      return  round(distance,3);
    END;
    

提交回复
热议问题