• 经纬度计算之身边团购功能实践


    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
            private const double EARTH_RADIUS = 6378.137;
            /// <summary>
            /// 根据经纬度计算距离 范围公里
            /// 例: 1.6126 => 1.6公里
            /// </summary>
            /// <param name="d"></param>
            /// <returns></returns>
            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;
            }

    前端时间开发类似美团的身边团购,现使用代码总结整理:

    --周边团购 user 新增字段
    --字段写入格式: { addr:"北京西站公交站", lng:"116.328316", lat:"39.902631" }
    --使用JavaScriptSerializer类解析。
    alter table [user] add AddrOften nvarchar(500) not null default('')
    alter table [user] add AddrHome nvarchar(500) not null default('')
    alter table [user] add AddrCompany nvarchar(500) not null default('')

    --定位坐标:116.327857,39.901028

              select dbo.fnGetDistance(39.901028,116.327857,39.906187,116.331097)

              select dbo.fnGetDistance(116.327857,39.901028,116.331097,39.906187)

    --2者误差200米左右
    --因为http://www.aspbc.com/tech/showtech.asp?id=1136
    --sql的函数参数上可以看出第一个参数为纬度,然后经度、纬度、经度;
    --而示例中使用 是经度为第一个参数,纬度为第二个参数、再次纬度、经度。
    --sql语句 也将使用第一种,纬度为第一个参数,经度为第二个参数。

    主要使用:

      1.BMap.Autocomplete实现自动完成。

      2.BMap.LocalSearch 对象的 setSearchCompleteCallback事件,以及分页数据。

      2.使用json传递处理地址以及经纬度。

    参考链接地址:

    http://api.map.baidu.com/library/SearchControl/1.4/examples/SearchControl.html

    http://www.aspbc.com/tech/showtech.asp?id=1136

    http://www.cnblogs.com/ycsfwhh/archive/2010/12/20/1911232.html

    http://blog.sina.com.cn/s/blog_a3c68906010136bg.html

    http://www.meituan.com/around/position/0

    经纬度计算工具:百度搜“经纬度计算距离.exe”

  • 相关阅读:
    解决Oracle SQL Developer无法连接远程服务器的问题
    [备忘] Automatically reset Windows Update components
    在ASP.NET MVC的Action中直接接受客户端发送过来的HTML内容片段
    Rehosting the Workflow Designer
    解决Onedrive经常无法访问的问题
    最好的简明NodeJS学习材料
    最好的Python简明教程
    在Linux(ubuntu server)上面安装NodeJS的正确姿势
    在Windows中安装NodeJS的正确姿势
    在Windows环境中开始Docker的学习和体验
  • 原文地址:https://www.cnblogs.com/lztkdr/p/aroundMap.html
Copyright © 2020-2023  润新知