我建议使用
Geography data type
--this is your table
CREATE TABLE Landmark (
Id int,
Name VARCHAR(100),
Latitude FLOAT,
Longitude FLOAT
)
INSERT Landmark VALUES
(1, 'Greek Restaurant', -72.374984, 41.274672),
(2, 'Italian Restaurant', -73.483947, 40.739283)
--this is better table to query
WITH GeographyLandmark AS
(
SELECT Id, Name, geography::STPointFromText('POINT(' + CAST(Latitude AS VARCHAR(20)) + ' ' + CAST(Longitude AS VARCHAR(20)) + ')', 4326) Location
FROM LandMark
)
--this query calculates distance between point and localizations in meters
SELECT Id, Name,
geography::STPointFromText('POINT(' + CAST(-74.009056 AS VARCHAR(20)) + ' ' + CAST(40.713744 AS VARCHAR(20)) + ')', 4326).STDistance(Location) Distance
FROM GeographyLandmark
结果:
Id Name Distance
----- -------------------- ----------------
1 Greek Restaurant 150944,610588657
2 Italian Restaurant 44456,82536079
参考文献:
STDistance
,
STPointFromText