问题
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