【问题标题】:radius search by latitude / longitude按纬度/经度进行半径搜索
【发布时间】:2013-03-15 18:14:17
【问题描述】:

我使用 mysql 找到了很多关于这个问题的答案,但我无法将任何内容转换为 ms sql 2008 可以使用的查询。我在数据库中的每一行都有一个经度和纬度列。我将有一个用户所在位置的纬度和经度。我希望能够找到距离用户纬度/经度 x 英里范围内的所有行。此外,当尝试使用我在 SO 上找到的其他查询时,我不断收到错误 - 'pow' is not a recognized built-in function name.,这很奇怪,因为我很确定我之前在 sql 2008 中使用过 pow。对此的任何帮助将不胜感激.到目前为止,这是最接近的。

select * from tbl_MyTable
WHERE (
POW( ( 69.1 * ( Longitude - @longitude ) * cos( @latitude / 57.3 ) ) , 2 ) + POW( ( 69.1 * ( Latitude - @latitude ) ) , 2 )
) < ( 5 *5 );

【问题讨论】:

    标签: sql-server database sql-server-2008 geolocation


    【解决方案1】:

    由于您使用的是 SQL 2008,因此请考虑使用本机地理空间功能。你可以做一些花哨的事情,比如:

    • 创建一个表示您的点的地理类型的持久计算列。
    • 在计算列上创建空间索引。这将使yourPoint.STDistance(@otherPoint) &lt;= @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;
    

    【讨论】:

    • 您有设置链接吗?我认为这是 ms sql 2012 中的新内容
    • 太棒了,谢谢伙计,这是一个更好的答案,我不想从德里克的接受答案中拿走,因为他在技术上确实回答了我的问题并且是第一个
    • ohhhhh- 等等,里面的 4326 是什么?北美需要 4152 吗??
    • 4226 是所谓的 SRID 或空间参考标识符。与许多事情一样,地理空间数据表示存在许多相互竞争的标准。 SQL Server 知道的所有 SRID 都位于名为 sys.spatial_reference_systems 的系统视图中。
    • 您可以在 SQL Server 2012 中持久化地理计算列。
    【解决方案2】:
    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
    

    精彩教程

    http://www.sql-server-helper.com/sql-server-2008/convert-latitude-longitude-to-geography-point.aspx

    【讨论】:

      【解决方案3】:

      我认为你想要 POWER 而不是 POW

      http://msdn.microsoft.com/en-us/library/ms174276.aspx

      【讨论】:

      • 这确实解决了这个问题,但是 Ben 写出了一个完整的索引更快更智能的方式来执行这个查询
      • 这一切都很好,用一位匿名象棋大师的话来说:“当我找到一个好棋步时,我会寻找更好的棋步”。
      猜你喜欢
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 2011-02-12
      相关资源
      最近更新 更多