C#实现微信结合百度api获取当前用户地理位置的方

时间:2022-12-09

本文实例讲述了C#实现微信结合百度api获取当前用户地理位置的方法。分享给大家供大家参考,具体如下:

操作步骤:

1. 先根据微信提供的接口文档获取到经纬度

参考文档地址:http://mp.weixin.qq.com/wiki/8/1b86529d05db9f960e48c3c7ca5be288.html

2. 由于直接使用微信获取到的经纬度,来使用百度地图api获取地址存在500~1000米的偏差,有时候定位不是很准确,所以需要转换成百度的坐标

参考文档地址:http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition

3. 根据百度api实现逆地址解析,获取当前位置

参考文档地址:http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding

具体实例:

该例子是mvc项目

1. WX_Location.cshtml页面:


@using System.Web.Optimization;
@{
 Layout = "~/Views/Shared/Master/_App.cshtml";
}
@section Head{
}
<div data-role="page">
 <div data-role="header" id="header" data-position="fixed">
  <h1>微信定位页面</h1>
 </div>
 <div data-role="content" class="ui-content">
  <form id="add" method="post" onsubmit="return false">
   <table border="0" cellpadding="0" cellspacing="0" class="register_table">
    <tr class='tr_bg neiy'>
<td class='tdl neiy'>定位:</td>
<td class='tdr'><input id='txt_dw' name='dw' type='text' placeholder='正在定位' class='td_input' value='' /></td></tr>
   </table>
   <div data-role="footer" data-position="fixed" data-id="footernav">
    <div data-role="navbar">
     <ul>
      <li><a href="" style=" rel="external nofollow" font-size: 1em;">返回主页</a></li>
     </ul>
    </div>
   </div>
   @Html.AntiForgeryToken()
  </form>
 </div>
</div>
@section scripts{
  @*微信js-sdk*@
 <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript"></script>
 <script type="text/javascript">
  //微信jssdk配置
  wx.config({
   debug: false,
   appId: "",
   timestamp: "@Html.Raw(ViewData["timestamp"])",
   nonceStr: "@Html.Raw(ViewData["nonceStr"])",
   signature: "@Html.Raw(ViewData["signature"])",
   jsApiList: [
     'checkJsApi',
     'onMenuShareTimeline',
     'onMenuShareAppMessage',
     'onMenuShareQQ',
     'onMenuShareWeibo',
     'hideMenuItems',
     'showMenuItems',
     'hideAllNonBaseMenuItem',
     'showAllNonBaseMenuItem',
     'translateVoice',
     'startRecord',
     'stopRecord',
     'onRecordEnd',
     'playVoice',
     'pauseVoice',
     'stopVoice',
     'uploadVoice',
     'downloadVoice',
     'chooseImage',
     'previewImage',
     'uploadImage',
     'downloadImage',
     'getNetworkType',
     'openLocation',
     'getLocation',
     'hideOptionMenu',
     'showOptionMenu',
     'closeWindow',
     'scanQRCode',
     'chooseWXPay',
     'openProductSpecificView',
     'addCard',
     'chooseCard',
     'openCard'
   ]
  });
  //wx.error(function (res) {
  // alert("错误:" + res);
  /