1. 程式人生 > >騰訊地圖H5位置獲取

騰訊地圖H5位置獲取

騰訊地圖H5位置獲取

  1. 直接ctrl+c 就行了
  2. 直接開啟
<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <title>前端定位模組</title>
   <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"
> <style> * { margin: 0; padding: 0; border: 0; } body { position: absolute; width: 100%; height: 100%; text-align: center; } #pos-area { background-color: #009DDC; margin-
bottom: 10px; width: 100%; overflow: scroll; text-align: left; color: white; } #demo { padding: 8px; font-size: small; } #btn-area { height: 100px; } button { margin-bottom: 10
px; padding: 12px 8px; width: 42%; border-radius: 8px; background-color: #009DDC; color: white; } </style> <script type="text/javascript" src="https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js"></script> </head> <body> <div id="pos-area"> <p id="demo">點選下面的按鈕,獲得對應資訊:<br /></p> </div> <div id="btn-area"> <button onClick="geolocation.getLocation(showPosition, showErr, options)">獲取精確定位資訊</button> <button onClick="geolocation.getIpLocation(showPosition, showErr)">獲取粗糙定位資訊</button> <button onClick="showWatchPosition()">開始監聽位置</button> <button onClick="showClearWatch()">停止監聽位置</button> </div> <script type="text/JavaScript"> var geolocation = new qq.maps.Geolocation("OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77", "myapp"); document.getElementById("pos-area").style.height = (document.body.clientHeight - 110) + 'px'; var positionNum = 0; var options = {timeout: 8000}; function showPosition(position) { positionNum ++; document.getElementById("demo").innerHTML += "序號:" + positionNum; document.getElementById("demo").appendChild(document.createElement('pre')).innerHTML = JSON.stringify(position, null, 4); document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight; }; function showErr() { positionNum ++; document.getElementById("demo").innerHTML += "序號:" + positionNum; document.getElementById("demo").appendChild(document.createElement('p')).innerHTML = "定位失敗!"; document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight; }; function showWatchPosition() { document.getElementById("demo").innerHTML += "開始監聽位置!<br /><br />"; geolocation.watchPosition(showPosition); document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight; }; function showClearWatch() { geolocation.clearWatch(); document.getElementById("demo").innerHTML += "停止監聽位置!<br /><br />"; document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight; }; </script> </body> </html>
  1. 還有一份
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <title>Geolocation Components Demo - zoom effect</title>
   <meta name="viewport" content="width=device-width,initial-scale=1,
   minimum-scale=1,maximum-scale=1,user-scalable=no">
   <style>
       * {margin: 0; padding: 0; border: 0;}
       body {
           position: absolute;
           width: 100%;
           height: 100%;
       }
       #geoPage, #markPage {
           position: relative;
       }
   </style>
</head>
<body>
   <!--  通過 iframe 嵌入前端定位元件,此處沒有隱藏定位元件,使用了定位元件的在定位中視覺特效  -->
   <iframe id="geoPage" width="100%" height="30%" frameborder=0 scrolling="no"
   src="https://apis.map.qq.com/tools/geolocation?key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myapp&effect=zoom"></iframe>

   <script type="text/JavaScript">
       var loc;
       var isMapInit = false;
       //監聽定位元件的message事件
       window.addEventListener('message', function(event) {
           loc = event.data; // 接收位置資訊
           console.log('location', loc);

           if(loc  && loc.module == 'geolocation') { //定位成功,防止其他應用也會向該頁面post資訊,需判斷module是否為'geolocation'
               var markUrl = 'https://apis.map.qq.com/tools/poimarker' +
               '?marker=coord:' + loc.lat + ',' + loc.lng +
               ';title:我的位置;addr:' + (loc.addr || loc.city) +
               '&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myapp';
               //給位置展示元件賦值
               document.getElementById('markPage').src = markUrl;
           } else { //定位元件在定位失敗後,也會觸發message, event.data為null
               alert('定位失敗');
           }

           /* 另一個使用方式
           if (!isMapInit && !loc) { //首次定位成功,建立地圖
               isMapInit = true;
               createMap(event.data);
           } else if (event.data) { //地圖已經建立,再收到新的位置資訊後更新地圖中心點
               updateMapCenter(event.data);
           }
           */
       }, false);
       //為防止定位元件在message事件監聽前已經觸發定位成功事件,在此處顯示請求一次位置資訊
       document.getElementById("geoPage").contentWindow.postMessage('getLocation', '*');

       //設定6s超時,防止定位元件長時間獲取位置資訊未響應
       setTimeout(function() {
           if(!loc) {
               //主動與前端定位元件通訊(可選),獲取粗糙的IP定位結果
           document.getElementById("geoPage")
               .contentWindow.postMessage('getLocation.robust', '*');
           }
       }, 6000); //6s為推薦值,業務呼叫方可根據自己的需求設定改時間,不建議太短
   </script>

   <!-- 接收到位置資訊後 通過 iframe 嵌入位置標註元件 -->
   <iframe id="markPage" width="100%" height="70%" frameborder=0 scrolling="no" src=""></iframe>
</body>
</html>