本文實例講述了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);
/
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!