使用SQL地理位置获取某个半径的位置

时间:2014-03-21 13:56:33

标签: sql sql-server geocoding geospatial

我有一个带有Geography类型的SQL表列:

create table dbo.Events
(
  Id int identity not null 
    constraint PK_Events_Id primary key clustered (Id),
  Localization geography not null
);

如何在半径40公里范围内获得所有活动?这可能吗?

谢谢你, 米格尔

2 个答案:

答案 0 :(得分:15)

假设您有要搜索的点的纬度和经度:

DECLARE @Origin GEOGRAPHY,
        -- distance defined in meters
        @Distance INTEGER = 40000;        

-- center point
SET @Origin = GEOGRAPHY::STGeomFromText('POINT(-122.084039 37.42227)', 4326);

-- return all rows from events in 40km radius
SELECT * FROM dbo.Events WHERE @Origin.STDistance(Localizaton) <= @Distance;

答案 1 :(得分:2)

DECLARE @h geography;
SET @h = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326); --set here GPS from where you want to search 40 km
SELECT Localization.STDistance(@h) from dbo.events

STDistance()的结果将以米为单位。