微信lbs---返回兩個經緯度座標點的距離

微信開發:lbs附近的商家,在數據庫裏記錄商家的座標,lbs設置裏管理搜索半徑,查詢的時候,查詢 客戶當前座標的半徑內的全部商家列表。
我的喜歡不同,我選擇了執行sql ,畢竟效果高點。微信開發必須得將就效率問題。否則等半天出不來,急死人,半天出不來結果,客戶直接走人。
很少說,直接上代碼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
相關文章
相關標籤/搜索