如何从 T-SQL 中的经纬度获取位置?

How to get location from latitude and longitude in T-SQL?

我有一个 locationId 映射 table 以及它们的中心纬度和中心经度值,如下所示-

| Location Id | Center_lat | Center_long |
|-------------|------------|-------------|
| 1           | 50.546     | 88.344      |
| 2           | 48.546     | 86.344      |
| 3           | 52.546     | 89.344      |

我有另一个 table,我正在为用户获取连续的经纬度位置数据,如下所示 -

+---------+------------+-------------+
| User Id | Center_lat | Center_long |
+---------+------------+-------------+
|     101 |     50.446 |      88.314 |
|     102 |     48.446 |      86.314 |
|     103 |     52.446 |      89.314 |
+---------+------------+-------------+

我想获取所有用户的位置标识,如果他们的纬度和经度值位于与位置标识对应的经纬度值的 1000 米以内。我怎样才能在 T-SQL?

中完成它

最终 table 应该像下面这样 -

+---------+------------+-------------+------------+
| User Id | Center_lat | Center_long | LocationId |
+---------+------------+-------------+------------+
|     101 |     50.546 |      88.344 |          1 |
|     102 |     48.546 |      86.344 |          2 |
|     103 |     52.546 |      89.344 |          3 |
+---------+------------+-------------+------------+

您可以使用将 latitude/longitude 对转换为 geography 对象,然后使用 stdistance():

select u.*, l.location_id
from users u
inner join locations l
    on geography::point(u.center_lat, u.center_long, 4326).stdistance(geography::point(l.center_lat, l.center_long, 4326)) < 1000

请注意,将此信息存储为 point 开头会更有效。