微信獲取用戶地理位置信息-微信jsapi接口

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.     
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.     <!-- www.vxzsk.com原創 -->  
  12.     <title>微信jsapi測試-V型知識庫</title>  
  13.     <meta name="viewport" content="width=320.1,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">  
  14.    <script src="http://res.wx.qq.com/open/js/jweixin-1.1.0.js"> </script>   
  15.    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>  
  16.         
  17.   </head>  
  18.       
  19.   <body>  
  20.   <center><h3>歡迎來到微信jsapi測試界面-V型知識庫</h3></center>  
  21.      <p>基礎接口之判斷當前客戶端是否支持指定的js接口</p>     
  22.      <input type="button" value="checkJSAPI" id="checkJsApi"><br>  
  23.          
  24.       <span class="desc" style="color: red">地理位置接口-使用微信內置地圖查看位置接口</span><br>  
  25.       <button class="btn btn_primary" id="openLocation">openLocation</button><br>  
  26.       <span class="desc" style="color: red">地理位置接口-獲取地理位置接口</span><br>  
  27.       <button class="btn btn_primary" id="getLocation">getLocation</button><br>  
  28.          
  29.      <div style="display: none;">   
  30.      <span class="desc" style="color: red">獲取網絡狀態接口</span><br>  
  31.       <button class="btn btn_primary" id="getNetworkType">getNetworkType</button><br>  
  32.       <h3 id="menu-image">圖像接口</h3>  
  33.       <span class="desc">拍照或從手機相冊中選圖接口</span><br>  
  34.       <button class="btn btn_primary" id="chooseImage">chooseImage</button><br>  
  35.       <span class="desc">預覽圖片接口</span><br>  
  36.       <button class="btn btn_primary" id="previewImage">previewImage</button><br>  
  37.       <span class="desc">上傳圖片接口</span><br>  
  38.       <button class="btn btn_primary" id="uploadImage">uploadImage</button><br>  
  39.       <span class="desc">下載圖片接口</span><br>  
  40.       <button class="btn btn_primary" id="downloadImage">downloadImage</button><br>  
  41.         
  42.   <br>  
  43.   顯示圖片<img alt="" src="" id="faceImg">  
  44.   </div>  
  45.       
  46.       
  47.   <script type="text/javascript">  
  48.   wx.config({    
  49.     debug: true, // 開啓調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時纔會打印。    
  50.     appId: '${appId}', // 必填,公衆號的唯一標識    
  51.     timestamp: '${ timestamp}' , // 必填,生成簽名的時間戳    
  52.     nonceStr: '${ nonceStr}', // 必填,生成簽名的隨機串    
  53.     signature: '${ signature}',// 必填,簽名,見附錄1    
  54.     jsApiList: ['checkJsApi',  
  55.                 'chooseImage',  
  56.                 'previewImage',  
  57.                  'uploadImage',  
  58.                  'downloadImage',  
  59.                   'getNetworkType',//網絡狀態接口  
  60.                   'openLocation',//使用微信內置地圖查看地理位置接口  
  61.                   'getLocation' //獲取地理位置接口  
  62.                ] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2    
  63. });    
  64.       
  65. wx.ready(function(){    
  66.     // 5 圖片接口  
  67.   // 5.1 拍照、本地選圖  
  68.   var images = {  
  69.     localId: [],  
  70.     serverId: []  
  71.   };  
  72.   document.querySelector('#chooseImage').onclick = function () {  
  73.     wx.chooseImage({  
  74.       success: function (res) {  
  75.         images.localId = res.localIds;  
  76.         alert('已選擇 ' + res.localIds.length + ' 張圖片');  
  77.          $("#faceImg").attr("src", res.localIds[0]);//顯示圖片到頁面上  
  78.       }  
  79.     });  
  80.   };  
  81.     
  82.   // 5.2 圖片預覽  
  83.   document.querySelector('#previewImage').onclick = function () {  
  84.     wx.previewImage({  
  85.       current: 'http://www.vxzsk.com/upload//bf04c9b5-5699-421d-900e-3b68bbe58a8920160816.jpg',  
  86.       urls: [  
  87.         'http://www.vxzsk.com/upload//bf04c9b5-5699-421d-900e-3b68bbe58a8920160816.jpg',  
  88.         'http://www.vxzsk.com/upload//bf04c9b5-5699-421d-900e-3b68bbe58a8920160816.jpg',  
  89.         'http://www.vxzsk.com/upload//bf04c9b5-5699-421d-900e-3b68bbe58a8920160816.jpg'  
  90.       ]  
  91.     });  
  92.   };  
  93.     
  94.   // 5.3 上傳圖片  
  95.   document.querySelector('#uploadImage').onclick = function () {  
  96.     if (images.localId.length == 0) {  
  97.       alert('請先使用 chooseImage 接口選擇圖片');  
  98.       return;  
  99.     }  
  100.     var i = 0length = images.localId.length;  
  101.     images.serverId = [];  
  102.     function upload() {  
  103.       wx.uploadImage({  
  104.         localId: images.localId[i],  
  105.         success: function (res) {  
  106.           i++;  
  107.           //alert('已上傳:' + i + '/' + length);  
  108.           images.serverId.push(res.serverId);  
  109.           if (i < length) {  
  110.             upload();  
  111.           }  
  112.         },  
  113.         fail: function (res) {  
  114.           alert(JSON.stringify(res));  
  115.         }  
  116.       });  
  117.     }  
  118.     upload();  
  119.   };  
  120.     
  121.   // 5.4 下載圖片  
  122.   document.querySelector('#downloadImage').onclick = function () {  
  123.     if (images.serverId.length === 0) {  
  124.       alert('請先使用 uploadImage 上傳圖片');  
  125.       return;  
  126.     }  
  127.     var i = 0length = images.serverId.length;  
  128.     images.localId = [];  
  129.     function download() {  
  130.       wx.downloadImage({  
  131.         serverId: images.serverId[i],  
  132.         success: function (res) {  
  133.           i++;  
  134.           alert('已下載:' + i + '/' + length);  
  135.           images.localId.push(res.localId);  
  136.           if (i < length) {  
  137.             download();  
  138.           }  
  139.         }  
  140.       });  
  141.     }  
  142.     download();  
  143.   };  
  144.       
  145.   // 6 設備信息接口  
  146.   // 6.1 獲取當前網絡狀態  
  147.   document.querySelector('#getNetworkType').onclick = function () {  
  148.     wx.getNetworkType({  
  149.       success: function (res) {  
  150.         alert(res.networkType);  
  151.       },  
  152.       fail: function (res) {  
  153.         alert(JSON.stringify(res));  
  154.       }  
  155.     });  
  156.   };  
  157.   //網絡接口結束  
  158.       
  159.   // 7 地理位置接口 開始  
  160.   // 7.1 查看地理位置  
  161.   document.querySelector('#openLocation').onclick = function () {  
  162.     wx.openLocation({  
  163.       latitude: 23.099994,  
  164.       longitude: 113.324520,  
  165.       name: 'TIT 創意園',  
  166.       address: '廣州市海珠區新港中路 397 號',  
  167.       scale: 14,  
  168.       infoUrl: 'http://weixin.qq.com'  
  169.     });  
  170.   };  
  171.     
  172.   // 7.2 獲取當前地理位置  
  173.   document.querySelector('#getLocation').onclick = function () {  
  174.     wx.getLocation({  
  175.       success: function (res) {  
  176.         alert(JSON.stringify(res));  
  177.       },  
  178.       cancel: function (res) {  
  179.         alert('用戶拒絕授權獲取地理位置');  
  180.       }  
  181.     });  
  182.   };  
  183.   // 7 地理位置接口 結束   
  184.       
  185. });    
  186.  //初始化jsapi接口 狀態  
  187. wx.error(function (res) {  
  188.   alert("調用微信jsapi返回的狀態:"+res.errMsg);  
  189. });  
  190.       
  191.  </script>  
  192.        
  193.   </body>  
  194. </html>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章