Get places in radius of a certain point using SQL geography

邮差的信 提交于 2019-12-21 04:49:29

问题


I have an SQL table column with the Geography type:

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

How can I get all events in a radius of 40 km? Is this possible?

Thank You, Miguel


回答1:


Assuming you have latitude and longitude of the point from which you want to search:

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;



回答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

The result of STDistance() will be in meters.



来源:https://stackoverflow.com/questions/22560832/get-places-in-radius-of-a-certain-point-using-sql-geography

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!