微信网页开发(8)--地理位置接口
生活随笔
收集整理的這篇文章主要介紹了
微信网页开发(8)--地理位置接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
點此查看 微信公眾號/微信網頁/微信支付/企業微信/小程序開發合集及源代碼下載
本文目錄
- 1. 背景
- 2. 代碼
- 3. 測試
1. 背景
微信網頁提供了獲取當前地理位置經緯度,以及通過內置地圖查看當前位置的接口。
官方接口說明如下:
// 獲取位置 wx.getLocation({type: 'wgs84', // 默認為wgs84的gps坐標,如果要返回直接給openLocation用的火星坐標,可傳入'gcj02'success: function (res) {var latitude = res.latitude; // 緯度,浮點數,范圍為90 ~ -90var longitude = res.longitude; // 經度,浮點數,范圍為180 ~ -180。var speed = res.speed; // 速度,以米/每秒計var accuracy = res.accuracy; // 位置精度} }); // 查看位置 wx.openLocation({latitude: 0, // 緯度,浮點數,范圍為90 ~ -90longitude: 0, // 經度,浮點數,范圍為180 ~ -180。name: '', // 位置名address: '', // 地址詳情說明scale: 1, // 地圖縮放級別,整型值,范圍從1~28。默認為最大infoUrl: '' // 在查看位置界面底部顯示的超鏈接,可點擊跳轉 });2. 代碼
編寫完整代碼如下:
<input type="button" value="查看位置" onclick="openLocation()"> |<input type="button" value="獲取位置" onclick="getLocation()"> |<script src="http://cdn.bootcdn.net/ajax/libs/jquery/1.9.1/jquery.min.js"></script><script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script><script>var apiList = [ 'checkJsApi', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'networkTest', 'openLocation', 'getLocation' ];$(function() {// 向后端請求配置信息$.ajax({type : "GET",url : "/wx-server/wxJsapiSignature",data : {url : location.href.split('#')[0]},dataType : "json",success : function(res) {configInfo(res);}});});// 通過wx.config注入配置信息function configInfo(res) {wx.config({debug : false, // 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時才會打印。appId : res.appId, // 必填,公眾號的唯一標識timestamp : res.timestamp, // 必填,生成簽名的時間戳nonceStr : res.nonceStr, // 必填,生成簽名的隨機串signature : res.signature,// 必填,簽名jsApiList : apiList// 必填,需要使用的JS接口列表});// 處理成功后回調wx.ready(function() {console.log("處理成功:");wx.checkJsApi({jsApiList : apiList,success : function(checkRes) {console.log("checkRes:", checkRes);}});});// 處理失敗后回調wx.error(function(err) {console.log("處理失敗:", err);});}// 查看位置function openLocation() {wx.openLocation({latitude : 36, // 緯度,浮點數,范圍為90 ~ -90longitude : 117, // 經度,浮點數,范圍為180 ~ -180。name : '', // 位置名address : '', // 地址詳情說明scale : 10, // 地圖縮放級別,整型值,范圍從1~28。默認為最大infoUrl : '' // 在查看位置界面底部顯示的超鏈接,可點擊跳轉});}// 獲取位置function getLocation() {wx.getLocation({type : 'wgs84', // 默認為wgs84的gps坐標,如果要返回直接給openLocation用的火星坐標,可傳入'gcj02'success : function(res) {console.log(res);var latitude = res.latitude; // 緯度,浮點數,范圍為90 ~ -90var longitude = res.longitude; // 經度,浮點數,范圍為180 ~ -180。}});}</script>3. 測試
通過微信開發者工具,點擊【獲取位置】,輸出如下:
使用微信打開對應網址,點擊【查看位置】,效果如下。注意此時使用開發者工具無法彈出內置地圖,需要使用微信客戶端。
總結
以上是生活随笔為你收集整理的微信网页开发(8)--地理位置接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言水仙花两种方法,c语言:3种方法;
- 下一篇: QPF BGA封装