单位米,算法来自百度http://api.map.baidu.com/library/GeoUtils/1.2/src/GeoUtils.js
CREATE FUNCTION [dbo].[ToDistance] ( @Lng1 FLOAT, @Lat1 FLOAT, @Lng2 FLOAT, @Lat2 FLOAT ) RETURNS FLOAT AS BEGIN -- Declare the return variable here DECLARE @Distance FLOAT SET @Lng1 = PI() * @Lng1 / 180 SET @Lat1 = PI() * @Lat1 / 180 SET @Lng2 = PI() * @Lng2 / 180 SET @Lat2 = PI() * @Lat2 / 180 -- Add the T-SQL statements to compute the return value here SET @Distance = 6370996.81 * ACOS((SIN(@Lat1) * SIN(@Lat2) + COS(@Lat1) * COS(@Lat2) * COS(@Lng2 - @Lng1))) / 1000 -- Return the result of the function RETURN @Distance END
单位米,算法来自高德
CREATE FUNCTION [dbo].[ToDistance] ( -- Add the parameters for the function here @Lng1 FLOAT,@Lat1 FLOAT,@Lng2 FLOAT,@Lat2 FLOAT ) RETURNS FLOAT AS BEGIN -- Declare the return variable here DECLARE @Distance FLOAT, @Sin FLOAT, @SIN2 FLOAT, @SIN3 FLOAT, @SIN4 FLOAT SET @Lng1 = PI() * @Lng1 / 180 SET @Lat1 = PI() * @Lat1 / 180 SET @Lng2 = PI() * @Lng2 / 180 SET @Lat2 = PI() * @Lat2 / 180 SET @Sin = Sin(@Lng1); SET @Sin2 = Sin(@Lat1); SET @Sin3 = Sin(@Lng2); SET @Sin4 = Sin(@Lat2); SET @Lng1 = Cos(@Lng1); SET @Lat1 = Cos(@Lat1); SET @Lng2 = Cos(@Lng2); SET @Lat2 = Cos(@Lat2); -- Add the T-SQL statements to compute the return value here SET @Distance = Asin(Sqrt(((POWER((@Lng1 * @Lat1 - @Lng2 * @Lat2), 2)) + (POWER((@Lat1 * @Sin - @Lat2 * @Sin3), 2))) + (POWER((@Sin2 - @Sin4), 2))) / 2) * 1.27420015798544E7 / 1000 -- Return the result of the function RETURN @Distance END