有多种算法可以计算球体上的距离,但我们使用以下算法:
create function GetDistance(
@latitudeFrom decimal(30,10),
@longitudeFrom decimal(30,10),
@latitudeTo decimal(30,10),
@longitudeTo decimal(30,10)
)
RETURNS float
AS
BEGIN
DECLARE @distance float
SET @distance = ROUND(6378.137 * ACOS(
convert(decimal(30,10),
(SIN(RADIANS(@latitudeFrom)) * SIN(RADIANS(@latitudeTo))) +
(COS(RADIANS(@latitudeFrom)) * COS(RADIANS(@latitudeTo)) *
COS(RADIANS(@longitudeTo) - RADIANS(@longitudeFrom))))), 15)
RETURN @distance
END
go
(其中 6378.137 - 是地球的半径)
所以现在当你可以计算地球上两点之间的距离时,你可以构建查询
select *
from Table1, Project
where dbo.GetDistance(
Table1.lat, Table1.lon,
Project.lat, Project.lon) < @YouRadius
@YouRadius - 你的圆的参数化半径