网上商城自动定位到最近的自提地点

其实就是根据经纬度来计算最短的距离

  <nav onclick="TabControl_Either_Go();" style="right:0%;">
                <div>
                    <img id="goGo_id" src="Pics/Go.png" alt="上门" />
                    <p id="titleGo_id">到店自取</p>
                </div>
            </nav>

这一个是执行的一个点击事件,点击后,执行一个js

function TabControl_Either_Go() {
    //GetCurrentLocation();
    ShowPositionBMap();
    //According to where store being, show other information else.
    $("#tabcontrol_body_delivery").css("display", "none");
    $("#tabcontrol_body_go").css("display", "block");
    $("#goGo_id").attr("src", "Pics/Go_red.png");
    $("#titleGo_id").css("color", "#ff5477");
    $("#imgDelivery_id").attr("src", "Pics/Delivery.png");
    $("#titleDelivery_id").css("color", "#999999");
}

function ShowPositionBMap() {
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (po) {
        var lat = po.point.lat;
        var lng = po.point.lng;
        $.ajax({
            url: "/weixin/mshop/tools/HandleAddress.ashx",
            type: "POST",
            data: { lat: lat, lng: lng, action: "FindShop" },
            dataType: "json",
            success: function (data) {
                if (data != String.empty) {
                    $("#go_address_stores").text(data[0]["name"]);
                }
                else {
                    $("#go_address_stores").text("请手动选择!");
                }
            },
        });
    }, { enableHighAccuracy: true })
}

 通过帮助类计算距离

 public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        String methodName = context.Request["action"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);
        if (method == null) throw new Exception("method is null");
        object obj = null;
        try
        {
            object[] parm = new object[1];
            parm[0] = context;
            obj = method.Invoke(this, parm);
        }
        catch (Exception ex)
        {
            obj = "{" + String.Format(""result":"{0}","msg":"{1}"", -1, "操作失败" + ex.Message) + "}";
        }
        context.Response.Clear();
        context.Response.Write(obj.ToString());
        context.Response.End(); 
        
        string methodype = context.Request["action"].ToString();
        if (methodype == "FindShop")
        {
            FindShop(context);
        }
    }
    //从数据库里面把自己的经纬度查找出来,再计算
    public String FindShop (HttpContext context) {
        HttpRequest request = context.Request;
        String sql = string.Format("select top 1 * from dbo.company order by ABS((CAST(lng as float)-{0})+(cast(lat as float)-{1}));",request["lng"],request["lat"]);
        var dataset = TeenySoft.Query.DBComm.GetDataSetBySql(sql, null, "Server=localhost;Database=MedYX;uid=sa;Password=123456;Persist Security Info=true;");
        
        if (dataset != null)
        {
            var dataTable = dataset.Tables[0];
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<Dictionary<String, Object>> list = new List<Dictionary<String, Object>>();
            Dictionary<String, Object> result = new Dictionary<String, Object>();
            result.Add("name", dataTable.Rows[0]["name"].ToString());
            list.Add(result);
            return js.Serialize(list);
        }
        return String.Empty;
    }
原文地址:https://www.cnblogs.com/superMay/p/6182444.html