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

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

  <nav οnclick="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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值