C#
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; }
public static double GetShortDistance(double lon1, double lat1, double lon2, double lat2) { double DEF_PI = 3.14159265359; // PI double DEF_2PI = 6.28318530712; // 2*PI double DEF_PI180 = 0.01745329252; // PI/180.0 double DEF_R = 6370693.5; // radius of earth double ew1, ns1, ew2, ns2; double dx, dy, dew; double distance; // 角度转换为弧度 ew1 = lon1 * DEF_PI180; ns1 = lat1 * DEF_PI180; ew2 = lon2 * DEF_PI180; ns2 = lat2 * DEF_PI180; // 经度差 dew = ew1 - ew2; // 若跨东经和西经180 度,进行调整 if (dew > DEF_PI) dew = DEF_2PI - dew; else if (dew < -DEF_PI) dew = DEF_2PI + dew; dx = DEF_R * Math.Cos(ns1) * dew; // 东西方向长度(在纬度圈上的投影长度) dy = DEF_R * (ns1 - ns2); // 南北方向长度(在经度圈上的投影长度) // 勾股定理求斜边长 distance = Math.Sqrt(dx * dx + dy * dy); return distance; }
SQL
select id,hotel_code,bname,image,serviceId,fjmoney,score,lon,lat,(2 * 6378.137* ASIN(SQRT(power(SIN(PI()*(28.984223-lat)/360),2) +COS(PI()*28.984223/180)* COS(lat * PI()/180)*power(SIN(PI()*(112.92671-lon)/360),2)))) as distance into #temp from hotelid
lon 经坐标
lat 纬坐标
lat 28.984223
lon 112.92671