微信lbs---返回两个经纬度坐标点的距离

时间:2014-05-26 02:11:13   收藏:0   阅读:996

微信开发:lbs附近的商家,在数据库里记录商家的坐标,lbs设置里管理搜索半径,查询的时候,查询 客户当前坐标的半径内的所有商家列表。
个人喜欢不一样,我选择了执行sql ,毕竟效果高点。微信开发必须得将就效率问题。不然等半天出不来,急死人,半天出不来结果,客户直接走人。
不多说,直接上代码

\\返回两个经纬度坐标点的距离(单位:米)
 
\\C#  方法
 
///<summary>返回两个经纬度坐标点的距离(单位:米) by Alex.Y</summary>
        ///<param name="Longtiude">来源坐标经度Y</param>
        ///<param name="Latitude">来源坐标经度X</param>
        ///<param name="Longtiude2">目标坐标经度Y</param>
        ///<param name="Latitude2">目标坐标经度X</param>
        ///<returns>返回距离(米)</returns>
        public double getMapDistance(double Longtiude, double Latitude, double Longtiude2, double Latitude2)
        {
 
            var lat1 = Latitude;
            var lon1 = Longtiude;
            var lat2 = Latitude2;
            var lon2 = Longtiude2;
            var earthRadius = 6371; //appxoximate radius in miles  6378.137
 
            var factor = Math.PI / 180.0;
            var dLat = (lat2 - lat1) * factor;
            var dLon = (lon2 - lon1) * factor;
            var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(lat1 * factor)
              * Math.Cos(lat2 * factor) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
 
            double d = earthRadius * c * 1000;
 
            return d;
 
        }
 
 
 
--返回两个经纬度坐标点的距离(单位:千米)
 
-- =============================================
-- Author:      alex.Y
-- Create date: getdate()
-- Description: 返回两个经纬度坐标点的距离(单位:千米)
--        ///<param name="@LngBegin">来源坐标经度Y</param>
--        ///<param name="@LatBegin">来源坐标经度X</param>
--        ///<param name="@LngEnd">目标坐标经度Y</param>
 --       ///<param name="@LatEnd">目标坐标经度X</param>
-- =============================================
 
CREATE FUNCTION [dbo].[ufn_GetMapDistance]
(@LngBegin  REAL, @LatBegin REAL, @LngEnd REAL, @LatEnd REAL)
       RETURNS FLOAT
       AS
BEGIN
       --距离(千米)  
       DECLARE @Distance      REAL
       DECLARE @EARTH_RADIUS  REAL
       --SET @EARTH_RADIUS =6378.137
       SET @EARTH_RADIUS =6371 
        
       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
        
        -- SELECT [dbo].[ufn_GetMapDistance](116.3130497932434100,39.9804086267150800,116.3731849193573000,39.9110159284269700)
 
END

  

微信lbs---返回两个经纬度坐标点的距离,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!