-
经纬度计算距离
网络 2014/10/25 11:48:36C = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2-lon1)
以上全部是弧度单位
Lon1 A点经度
Lat1 A点纬度
Lon2 B点经度
Lat2 B点纬度
从google maps的脚本里扒了段代码,没准啥时会用上。大家一块看看是怎么算的。
private const double EARTH_RADIUS = 6378.137; private static double rad(double d) { return d * Math.PI / 180.0; } public static double GetDistance(double lat1, double lng1, double lat2, double lng2) { double radLat1 = rad(lat1); double radLat2 = rad(lat2); double a = radLat1 - radLat2; double b = rad(lng1) - rad(lng2); double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a/2),2) + Math.Cos(radLat1)*Math.Cos(radLat2)*Math.Pow(Math.Sin(b/2),2))); s = s * EARTH_RADIUS; s = Math.Round(s * 10000) / 10000; return s; }
//又一个JS版本
var EARTH_RADIUS = 6378137.0; //单位M var PI = Math.PI; function getRad(d){ return d*PI/180.0; } /** * caculate the great circle distance * @param {Object} lat1 * @param {Object} lng1 * @param {Object} lat2 * @param {Object} lng2 */ function getGreatCircleDistance(lat1,lng1,lat2,lng2){ var radLat1 = getRad(lat1); var radLat2 = getRad(lat2); var a = radLat1 - radLat2; var b = getRad(lng1) - getRad(lng2); var s = 2*Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2))); s = s*EARTH_RADIUS; s = Math.round(s*10000)/10000.0; return s; }
当然,我们都知道,地球其实并不是一个真正的圆球体,而是椭球,所以有了下面的公式:
/** * approx distance between two points on earth ellipsoid * @param {Object} lat1 * @param {Object} lng1 * @param {Object} lat2 * @param {Object} lng2 */ function getFlatternDistance(lat1,lng1,lat2,lng2){ var f = getRad((lat1 + lat2)/2); var g = getRad((lat1 - lat2)/2); var l = getRad((lng1 - lng2)/2); var sg = Math.sin(g); var sl = Math.sin(l); var sf = Math.sin(f); var s,c,w,r,d,h1,h2; var a = EARTH_RADIUS; var fl = 1/298.257; sg = sg*sg; sl = sl*sl; sf = sf*sf; s = sg*(1-sl) + (1-sf)*sl; c = (1-sg)*(1-sl) + sf*sl; w = Math.atan(Math.sqrt(s/c)); r = Math.sqrt(s*c)/w; d = 2*w*a; h1 = (3*r -1)/2/c; h2 = (3*r +1)/2/s; return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg)); }
SQLSERVER根据经纬度计算距离差
SQL SERVER 根据地图经纬度计算距离的公式
go --创建经纬度距离计算函数 CREATEFUNCTION [dbo].[fnGetDistance] --LatBegin 开始经度 --LngBegin 开始维度 (@LatBegin REAL, @LngBegin REAL, @LatEnd REAL, @LngEnd REAL) RETURNSFLOAT AS BEGIN --距离(千米) DECLARE @Distance REAL DECLARE @EARTH_RADIUS REAL SET @EARTH_RADIUS = 6378.137 DECLARE @RadLatBegin REAL, @RadLatEnd REAL, @RadLatDiff REAL, @RadLngDiff REAL SET @RadLatBegin = @LatBegin *PI()/ 180.0 SET @RadLatEnd = @LatEnd *PI()/ 180.0 SET @RadLatDiff = @RadLatBegin - @RadLatEnd SET @RadLngDiff = @LngBegin *PI()/ 180.0 - @LngEnd *PI()/ 180.0 SET @Distance = 2 *ASIN( SQRT( POWER(SIN(@RadLatDiff / 2), 2)+COS(@RadLatBegin)*COS(@RadLatEnd) *POWER(SIN(@RadLngDiff / 2), 2) ) ) SET @Distance = @Distance * @EARTH_RADIUS --SET @Distance = Round(@Distance * 10000) / 10000 RETURN @Distance END @Distance的单位为:千米
阅读(3548) 分享(0)
上一篇: 无线路由300M与实际网速的区别
下一篇: 腾讯是如何一刀刀,在15年间干死那些竞争对手?