mysql数据库计算
#公式如下,单位米: #第一点经纬度:lng1 lat1 #第二点经纬度:lng2 lat2 #round(6378.138*2*asin(sqrt(pow(sin((lat1*pi()/180-lat2*pi()/180)/2),2)+cos(lat1*pi()/180)*cos(lat2*pi()/180)*pow(sin( (lng1*pi()/180-lng2*pi()/180)/2),2)))*1000) #列如 SET @lat = 30.699799; SET @lng = 104.063393; SELECT id, round(6378.138*2*asin(sqrt(pow(sin((lat*pi()/180-@lat*pi()/180)/2),2)+cos(lat*pi()/180)*cos(@lat*pi()/180)*pow(sin( (lng*pi()/180-@lng*pi()/180)/2),2)))*1000) AS distance FROM markers HAVING distance < 1000 ORDER BY distance;
.NET计算
private static readonly double EARTH_RADIUS = 6378.137;//地球半径 /// <summary> /// 根据提供的两个经纬度计算距离(米) /// </summary> /// <param name="lng1">经度1</param> /// <param name="lat1">纬度1</param> /// <param name="lng2">经度2</param> /// <param name="lat2">纬度2</param> /// <returns></returns> 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 * 1000; } private static double rad(double d) { return d * Math.PI / 180.0; }