调用微信JsAPI端获取位置
public partial class test : BasePage { protected test() { AccessPage = PageWebType.WX; } protected string app_Id { get { return ViewState["app_Id"].ToString(); } set { ViewState["app_Id"] = value; } } protected string timestamp { get { return ViewState["timestamp"].ToString(); } set { ViewState["timestamp"] = value; } } protected string nonceStr { get { return ViewState["nonceStr"].ToString(); } set { ViewState["nonceStr"] = value; } } protected string signature { get { return ViewState["signature"].ToString(); } set { ViewState["signature"] = value; } } protected string ticket { get { return ViewState["ticket"].ToString(); } set { ViewState["ticket"] = value; } } protected string open_id { get { return ViewState["open_id"].ToString(); } set { ViewState["open_id"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { InitData(); lblInfo.Text = GetPointDistance(22.724623, 113.31403, 22.72464, 113.31404).ToString(); } } private void InitData() { //绑定js接口调用参数 var url = System.Configuration.ConfigurationManager.AppSettings["RootUrl"].ToString(); if (url.Length > 0) { var query = Request.Url.Query; var strUrl = url + "/app/css/test.aspx" + query; JsSignatureModel jsModel = new JsAPIOprate().GetJsApiSignature(CssUnionID, strUrl); if (jsModel != null) { app_Id = jsModel.app_id; signature = jsModel.signature; nonceStr = jsModel.noncestr; timestamp = jsModel.timestamp; ticket = jsModel.ticket; } } open_id = GetQueryString("open_id"); hideOpen_id.Value = open_id; } private const double EARTH_RADIUS = 6378.137;//地球半径 private static double Rad(double d) { return d * Math.PI / 180.0; } /// <summary> /// 返回两经纬度距离(千米) /// </summary> /// <param name="lat1">纬度1</param> /// <param name="lng1">经度1</param> /// <param name="lat2">纬度2</param> /// <param name="lng2">经度2</param> /// <returns></returns> private double GetPointDistance(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; } }
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"><title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no;" /> <script type="text/javascript" src="../../js/jquery.1.11.1.min.js"></script> <script type="text/javascript" src="../../js/jweixin-1.0.0.js"></script> <script type="text/javascript"> $(function () { wx.config({ debug: false, appId: '<%=app_Id%>', timestamp: <%=timestamp%>, nonceStr: '<%=nonceStr%>', signature: '<%=signature%>', jsApiList: [ 'checkJsApi', 'getLocation' ] }); wx.ready(function () { var isCheck=false; wx.checkJsApi({ jsApiList: [ 'checkJsApi', 'getLocation' ], success: function (res) { //alert(JSON.stringify(res)); isCheck=res.checkResult.getLocation; } }); document.querySelector('#btnLocation').onclick = function () { if(!isCheck){ msgtip("您的微信版本不支持获取位置!"); return; } wx.getLocation({ success: function (res) { alert(JSON.stringify(res)); }, cancel: function (res) { alert('发生错误'); } }); }; }); wx.error(function (res) { alert(res.errMsg); }); }); </script> </head> <body> <form id="form1" runat="server"> <header class="ui-header ui-header-positive ui-border-b"> <i class="ui-icon-return" onclick="history.back()"></i><h1>获取地址位置</h1><a href="Default.aspx?open_id=" class="sp-back sp-home"></a> </header> <div class="panel-body" style="margin-top:45px;"> <input type="button" id="btnLocation" value="取得位置" /> <asp:Literal ID="lblInfo" runat="server"></asp:Literal> </div> <div class="ui-poptips ui-poptips-warn" id="divInfo" style="display:none;"> <div class="ui-poptips-cnt"><i></i><span id="spntool"></span></div><input type="hidden" id="hideCard_no" /> </div><div id="datePlugin"></div><asp:HiddenField ID="hideOpen_id" runat="server" /> </form> </body> </html>