Sco*_*lby 20 database sql-server geolocation sql-server-2008
我已经使用mysql找到了一堆关于这个问题的答案,但我无法将任何内容转换为ms sql 2008可以使用的查询.我有一个数据库中每一行的经度和纬度列.我将拥有用户所在位置的经纬度.我希望能够找到距离用户纬度/经度x英里范围内的所有行.此外,当我尝试使用我发现的其他查询时,我一直得到错误 - 'pow' is not a recognized built-in function name. 这很奇怪,因为我很确定我pow之前在sql 2008中使用过.对此有任何帮助将非常感激.到目前为止,这是最接近的可能.
select * from tbl_MyTable
WHERE (
POW( ( 69.1 * ( Longitude - @longitude ) * cos( @latitude / 57.3 ) ) , 2 ) + POW( ( 69.1 * ( Latitude - @latitude ) ) , 2 )
) < ( 5 *5 );
Run Code Online (Sandbox Code Playgroud)
Ben*_*hul 37
由于您使用的是SQL 2008,请考虑使用本机地理空间功能.你可以做一些奇特的事情:
yourPoint.STDistance(@otherPoint) <= @distance有效像这样:
alter table [yourTable] add [p] as geography::Point(Latitude, Longitude, 4326) persisted;
create spatial index [yourSpatialIndex] on [yourTable] ([p])
declare @Latitude float = <somevalue>, @Longitude float = <somevalue>;
declare @point geography = geography::Point(@Latitude, @Longitude, 4326);
declare @distance int = <distance in meters>;
select * from [yourTable] where @point.STDistance([p]) <= @distance;
Run Code Online (Sandbox Code Playgroud)
DECLARE @CurrentLocation geography;
SET @CurrentLocation = geography::Point(12.822222, 80.222222, 4326)
SELECT * , Round (GeoLocation.STDistance(@CurrentLocation ),0) AS Distance FROM [Landmark]
WHERE GeoLocation.STDistance(@CurrentLocation )<= 2000 -- 2 Km
Run Code Online (Sandbox Code Playgroud)
精彩的教程
http://www.sql-server-helper.com/sql-server-2008/convert-latitude-longitude-to-geography-point.aspx