find the nearest location in ms-sql

前端 未结 3 1030
故里飘歌
故里飘歌 2021-02-08 18:10

I send these paramaters my script : Latitude : 41.0186 Longitude : 28.964701 (it is sample). i want to find nearest location\'s name. how to do this? (query\'s where code\'s mus

相关标签:
3条回答
  • 2021-02-08 18:16

    I think this is not so easy as it feels. You must do some trigonometry calculations to get the nearest positsion to your location.

    Found nice Javascript example:

    var R = 6371; // km Radius of earth
    var dLat = (lat2-lat1).toRad();
    var dLon = (lon2-lon1).toRad(); 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
            Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c;
    

    It gives you the distance between the points.

    Or you can try to pass in a sort order of the squared delta values of the long and lats:

    ((<lat> - LAT_COLUMN) * (<lat> - LAT_COLUMN) +
     (<lng> - LNG_COLUMN) * (<lng> - LNG_COLUMN))
    
    0 讨论(0)
  • 2021-02-08 18:30

    Use this function

    CREATE FUNCTION dbo.DictanceKM(@lat1 FLOAT, @lat2 FLOAT, @lon1 FLOAT, @lon2 FLOAT)
    RETURNS FLOAT 
    AS
    BEGIN
    
        RETURN ACOS(SIN(PI()*@lat1/180.0)*SIN(PI()*@lat2/180.0)+COS(PI()*@lat1/180.0)*COS(PI()*@lat2/180.0)*COS(PI()*@lon2/180.0-PI()*@lon1/180.0))*6371
    END
    

    You may order by this function, BUT on large datasets it will be very slow, so try to prefilter the recordset

    UPD:

    Using @chopikadze's test data:

    declare @lat float, @lng float
    select @lat = 41.0186, @lng = 28.964701
    
    declare @Location table(Latitude float, Longtitude float, Name nvarchar(50))
    insert into @Location(Latitude, Longtitude, Name) values (41.0200500000, 40.5234490000, 'a')
    insert into @Location(Latitude, Longtitude, Name) values (41.0185714000, 37.0975924000, 'b')
    insert into @Location(Latitude, Longtitude, Name) values (41.0184913000, 34.0373739000, 'c')
    insert into @Location(Latitude, Longtitude, Name) values (41.0166667000, 39.5833333000, 'd')
    insert into @Location(Latitude, Longtitude, Name) values (41.0166667000, 28.9333333000, 'e')
    
    SELECT ABS(dbo.DictanceKM(@lat, Latitude, @lng, Longtitude)) DistanceKm, * FROM @Location
    ORDER BY ABS(dbo.DictanceKM(@lat, Latitude, @lng, Longtitude))
    

    Assuming that the Earth is NOT a geoid, but the round ball, if you need under 1m exact formula - I can find it, don't have it with me

    0 讨论(0)
  • 2021-02-08 18:43
    declare @latitude float, @longitude float
    select @latitude = 41.0186, @longitude = 28.964701
    
     SELECT [Name]   --, other columns
          ,Distance
          from
          (
          select 
          [Name]       --, other columns 
           ,( 3959 * acos( cos( radians(@latitude) ) * cos( radians( [Lattitude] ) ) * cos( radians( [Longitude] )
           - radians(@longitude) ) + sin( radians(@latitude) ) * sin( radians( [Lattitude] ) ) ) ) 
           AS Distance  FROM [dbo].[Location]
       ) as x   
        where Distance < 5   
      order by distance
    
    0 讨论(0)
提交回复
热议问题