Ei kuvausta

ceshi.html 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>Geolocation Components Demo - zoom effect</title>
  5. <meta name="viewport" content="width=device-width,initial-scale=1,
  6. minimum-scale=1,maximum-scale=1,user-scalable=no">
  7. <style>
  8. * {margin: 0; padding: 0; border: 0;}
  9. body {
  10. position: absolute;
  11. width: 100%;
  12. height: 100%;
  13. }
  14. #geoPage, #markPage {
  15. position: relative;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <!-- 通过 iframe 嵌入前端定位组件,此处没有隐藏定位组件,使用了定位组件的在定位中视觉特效 -->
  21. <iframe id="geoPage" width="100%" height="30%" frameborder=0 scrolling="no"
  22. src="https://apis.map.qq.com/tools/geolocation?key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myapp&effect=zoom"></iframe>
  23. <script type="text/JavaScript">
  24. var loc;
  25. var isMapInit = false;
  26. //监听定位组件的message事件
  27. window.addEventListener('message', function(event) {
  28. loc = event.data; // 接收位置信息
  29. console.log('location', loc);
  30. if(loc && loc.module == 'geolocation') { //定位成功,防止其他应用也会向该页面post信息,需判断module是否为'geolocation'
  31. var markUrl = 'https://apis.map.qq.com/tools/poimarker' +
  32. '?marker=coord:' + loc.lat + ',' + loc.lng +
  33. ';title:我的位置;addr:' + (loc.addr || loc.city) +
  34. '&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myapp';
  35. //给位置展示组件赋值
  36. document.getElementById('markPage').src = markUrl;
  37. } else { //定位组件在定位失败后,也会触发message, event.data为null
  38. alert('定位失败');
  39. }
  40. /* 另一个使用方式
  41. if (!isMapInit && !loc) { //首次定位成功,创建地图
  42. isMapInit = true;
  43. createMap(event.data);
  44. } else if (event.data) { //地图已经创建,再收到新的位置信息后更新地图中心点
  45. updateMapCenter(event.data);
  46. }
  47. */
  48. }, false);
  49. //为防止定位组件在message事件监听前已经触发定位成功事件,在此处显示请求一次位置信息
  50. document.getElementById("geoPage").contentWindow.postMessage('getLocation', '*');
  51. //设置6s超时,防止定位组件长时间获取位置信息未响应
  52. setTimeout(function() {
  53. if(!loc) {
  54. //主动与前端定位组件通信(可选),获取粗糙的IP定位结果
  55. document.getElementById("geoPage")
  56. .contentWindow.postMessage('getLocation.robust', '*');
  57. }
  58. }, 6000); //6s为推荐值,业务调用方可根据自己的需求设置改时间,不建议太短
  59. </script>
  60. <!-- 接收到位置信息后 通过 iframe 嵌入位置标注组件 -->
  61. <iframe id="markPage" width="100%" height="70%" frameborder=0 scrolling="no" src=""></iframe>
  62. </body>
  63. </html>