问题描述
我在经度和纬度坐标中有位置.我的目标是最终能够从 myTable 中选择距离小于 2km 的所有行.
I have locations in longitude and latitude coordinates. My goal is eventually to be able to select all rows from myTable Where distance is less than 2km.
如何使用经度和纬度来存储地理列中的位置?(因为它应该只有一个地理点而不是两个,对吗?一个不是经度,一个是纬度?)
How can one use the longitude and latitude to store location within a geography column?(because it's supposed to be only one geographic point not two right? not one for longitude and one for latitude?)
既然我已经有了地理点,我该如何选择特定距离(在我的情况下为 2 公里)内的所有行?
Now that I've got the geography points, how can i select all the rows within a specific distance(in my case 2km)?
推荐答案
您可以使用 geography::STPointFromText
/geography::Point
到将经度和纬度存储在地理数据类型中.
You can use geography::STPointFromText
/ geography::Point
to store longitude and latitude in a geography datatype.
SELECT geography::STPointFromText('POINT(' + CAST([Longitude] AS VARCHAR(20)) + ' ' + CAST([Latitude] AS VARCHAR(20)) + ')', 4326)
或
SELECT geography::Point(Latitude, Longitude , 4326)
参考链接:
既然我已经有了地理点,我该如何选择特定距离(在我的情况下为 2 公里)内的所有行?
您可以使用 STDistance
之类的这个.
You can use STDistance
like this.
DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POINT(-122.35900 47.65129)', 4326);
SET @h = geography::STGeomFromText('POINT(-122.34720 47.65100)', 4326);
SELECT @g.STDistance(@h);
参考链接:
在 sqlserver 中使用 Geography 数据类型的两点之间的距离2008 年?
插入查询
DECLARE @GeoTable TABLE
(
id int identity(1,1),
location geography
)
--Using geography::STGeomFromText
INSERT INTO @GeoTable
SELECT geography::STGeomFromText('POINT(-122.35900 47.65129)', 4326)
--Using geography::Point
INSERT INTO @GeoTable
SELECT geography::Point(47.65100,-122.34720, 4326);
获取距离查询
DECLARE @DistanceFromPoint geography
SET @DistanceFromPoint = geography::STGeomFromText('POINT(-122.34150 47.65234)', 4326);
SELECT id,location.Lat Lat,location.Long Long,location.STDistance(@DistanceFromPoint) Distance
FROM @GeoTable;
这篇关于如何存储经度&纬度作为 sql server 2014 中的地理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!