这是一个示例查询,它返回供应商 lat/lng 和 lat/lng 对数据库之间的距离:
DECLARE
@Latitude float,
@Longitude float
SET @Latitude = 46.51257000000000
SET @Longitude = -84.33660900000000
DECLARE @SampleData TABLE
(
Name varchar(100),
Lat float,
Lng float
)
INSERT INTO @SampleData
(Name, Lat, Lng)
SELECT 'Point1', 46.52450048415351,-84.35223018530274
UNION
SELECT 'Point2', 46.51835838382206,-84.30279170874024
UNION
SELECT 'Point3', 46.522138220045285,-84.2622796237793
UNION
SELECT 'Point4', 46.54056115608927,-84.223140829834
SELECT
Name,
Lat,
Lng,
-- using spherical law of cosines, where 6371 is the earth's radius in km. Returns km.
Distance = ACOS(SIN(RADIANS(@Latitude))*SIN(RADIANS(Lat))+COS(RADIANS(@Latitude))*COS(RADIANS(Lat))*COS(RADIANS(Lng-@Longitude)))*6371
FROM
@SampleData