Skip to content Skip to sidebar Skip to footer

Given Longitude And Latitude Of Two Areas, How To Find The Distance Between Them In Meters. How To Query In Sql..?

I basically have two table with ID, Area.Longitude and latitude column. I need to find the difference in distances that are more than 20 meters apart for each area. Table 1 ##[ID]

Solution 1:

This is the oracle function We use in our projects to calculate distance. You please modify syntax accordingly to suit mysql / sql-server

create or replace
FUNCTION CALC_DISTANCE (Lat1 IN NUMBER,
                                     Lon1 IN NUMBER,
                                     Lat2 IN NUMBER,
                                     Lon2 IN NUMBER) RETURN NUMBER IS
 -- Convert degrees to radians
 DEGTORAD NUMBER := 57.29577951;
 --Radius NUMBER := 6387.7;  -- For km
   Radius NUMBER := 6387700  -- For metres

BEGIN
  RETURN(NVL(Radius,0) * ACOS((sin(NVL(Lat1,0) / DegToRad) * SIN(NVL(Lat2,0) / DegToRad)) +
        (COS(NVL(Lat1,0) / DegToRad) * COS(NVL(Lat2,0) / DegToRad) *
         Cos(Nvl(Lon2,0) / Degtorad - Nvl(Lon1,0)/ Degtorad))));
END;

To use this function to get in meters, you can probably use,

SELECT
CALC_DISTANCE ('51.51141557' ,'-0.138341652',
              '51.50819747', '-0.141020749') AS DISTANCE
FROM DUAL;

Post a Comment for "Given Longitude And Latitude Of Two Areas, How To Find The Distance Between Them In Meters. How To Query In Sql..?"