计算地理位置之间的距离

 .net 根据坐标,计算指定范围内的最大最小经纬度、根据两点间经纬度坐标(double值),计算两点间距离(注意:这里的单位是米)

 #region 根据坐标,计算指定范围内的最大最小经纬度、根据两点间经纬度坐标(double值),计算两点间距离,单位为米

        private static double PI = Math.PI; //3.14159265;  //π
        private static double EARTH_RADIUS = 6378137;    //地球半径
        private static double RAD = Math.PI / 180.0;   //   π/180



        #region 根据坐标,计算指定范围内的最大最小经纬度
        /// <summary>
        /// 根据坐标,计算指定范围内的最大最小经纬度
        /// </summary>
        /// <param name="lng">经度</param>
        /// <param name="lat">纬度</param>
        /// <param name="raidus">范围(米)</param>
        /// <returns>返回最大、最小经纬度minLng, minLat, maxLng, maxLat</returns>
        public double[] getAround(double lng, double lat, int raidus)
        {
            //The circumference of the earth is 24,901 miles.
            //24,901/360 = 69.17 miles / degree  

            Double latitude = lat;
            Double longitude = lng;

            Double degree = (24901 * 1609) / 360.0;   //地球的周长是24901英里
            double raidusMile = raidus;

            //先计算纬度
            Double dpmLat = 1 / degree;
            Double radiusLat = dpmLat * raidusMile;
            Double minLat = latitude - radiusLat;
            Double maxLat = latitude + radiusLat;

            //计算经度
            Double mpdLng = degree * Math.Cos(latitude * (PI / 180));  //纬度的余弦
            Double dpmLng = 1 / mpdLng;
            Double radiusLng = dpmLng * raidusMile;
            Double minLng = longitude - radiusLng;
            Double maxLng = longitude + radiusLng;
            //System.out.println("["+minLat+","+minLng+","+maxLat+","+maxLng+"]");
                               //最小经度,最小纬度,最大经度,最大纬度
            return new double[] { minLng, minLat, maxLng, maxLat };
        }
        #endregion


        #region 根据两点间经纬度坐标(double值),计算两点间距离,单位为米
        /// <summary>
        /// 根据两点间经纬度坐标(double值),计算两点间距离,单位为米
        /// </summary>
        /// <param name="lng1">经度1</param>
        /// <param name="lat1">纬度1</param>
        /// <param name="lng2">经度2</param>
        /// <param name="lat2">纬度2</param>
        /// <returns>返回距离(米)</returns>
        public double getDistance(double lng1, double lat1, double lng2, double lat2)
        {
            double radLat1 = lat1 * RAD;  // // RAD=π/180
            double radLat2 = lat2 * RAD;
            double a = radLat1 - radLat2;
            double b = (lng1 - lng2) * RAD;
            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;
        }

        #endregion

        #endregion


 

sql:计算地球上两个坐标点(经度,纬度)之间距离sql函数(注意:这里的单位是千米)

go   


--计算地球上两个坐标点(经度,纬度)之间距离sql函数 
 CREATE FUNCTION [dbo].[fnGetDistance](@LatBegin REAL, @LngBegin REAL, @LatEnd REAL, @LngEnd REAL) RETURNS FLOAT 

   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 

sql转自:http://www.aspbc.com/tech/showtech.asp?id=1136
百度地图获取两点间的距离在线测试:http://developer.baidu.com/map/jsdemo.htm#a6_1

 

posted @ 2015-03-15 20:51  hellocjr  阅读(7236)  评论(0编辑  收藏  举报