map.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>Map</title>
  6. <style>
  7. html {
  8. height: 100%
  9. }
  10. body {
  11. height: 100%;
  12. margin: 0;
  13. padding: 0;
  14. background-color: #FFF
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="mapContent" style="width:100%; height:100%"></div>
  20. <!--<script charset="utf-8" src="//api.map.baidu.com/api?ak=&v=1.3&services=true&s=1"></script>-->
  21. <script>
  22. window.onload = function () {
  23. var req = new XMLHttpRequest();
  24. req.open('GET', "/addons/umeditor/index/get_map_config", false);
  25. req.onreadystatechange = function () {
  26. if (req.readyState == 4) {
  27. try {
  28. var params = JSON.parse(req.responseText);
  29. loadScript(params.baidumapkey || '');
  30. } catch (e) {
  31. }
  32. }
  33. };
  34. req.send(null);
  35. };
  36. function loadScript(key) {
  37. var script = document.createElement("script");
  38. script.src = "https://api.map.baidu.com/api?v=3.0&ak=" + (getParam("key") || key) + "&callback=initialize";
  39. document.body.appendChild(script);
  40. }
  41. // window.onload = loadScript;
  42. var centerParam = getParam('center') || '116.404413,39.903536';
  43. var zoomParam = getParam('zoom') || 11;
  44. var widthParam = getParam('width') || 558;
  45. var heightParam = getParam('height') || 360;
  46. var markersParam = getParam('markers') || '';
  47. //创建和初始化地图函数:
  48. function initialize() {
  49. var mapContent = document.getElementById('mapContent');
  50. mapContent.style.width = widthParam + 'px';
  51. mapContent.style.height = heightParam + 'px';
  52. createMap();//创建地图
  53. setMapEvent();//设置地图事件
  54. addMapControl();//向地图添加控件
  55. // 创建标注
  56. if (markersParam) {
  57. var markersArr = markersParam.replace(/[\s]+/g, '').split(',');
  58. var point = new BMap.Point(markersArr[0], markersArr[1]);
  59. var marker = new BMap.Marker(point);
  60. map.addOverlay(marker); // 将标注添加到地图中
  61. }
  62. var iframe = getSelfIframe();
  63. var UM = parent.UM;
  64. var editor = getEditor();
  65. if (iframe && UM && editor) { //在编辑状态下
  66. setMapListener();//地图改变修改外层的iframe标签src属性
  67. } else {
  68. // document.focus();
  69. }
  70. }
  71. //创建地图函数:
  72. function createMap() {
  73. var map = new BMap.Map("mapContent");//在百度地图容器中创建一个地图
  74. var centerArr = centerParam.replace(/[\s]+/g, '').split(',');
  75. var point = new BMap.Point(centerArr[0], centerArr[1]);//定义一个中心点坐标
  76. map.centerAndZoom(point, zoomParam);//设定地图的中心点和坐标并将地图显示在地图容器中
  77. window.map = map;//将map变量存储在全局
  78. }
  79. //地图事件设置函数:
  80. function setMapEvent() {
  81. map.enableDragging();//启用地图拖拽事件,默认启用(可不写)
  82. map.enableScrollWheelZoom();//启用地图滚轮放大缩小
  83. map.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写)
  84. map.enableKeyboard();//启用键盘上下左右键移动地图
  85. }
  86. //地图控件添加函数:
  87. function addMapControl() {
  88. //向地图中添加缩放控件
  89. var ctrl_nav = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_LARGE});
  90. map.addControl(ctrl_nav);
  91. //向地图中添加缩略图控件
  92. var ctrl_ove = new BMap.OverviewMapControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: 1});
  93. map.addControl(ctrl_ove);
  94. //向地图中添加比例尺控件
  95. var ctrl_sca = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});
  96. map.addControl(ctrl_sca);
  97. }
  98. //获取URL参数
  99. function getParam(name) {
  100. return location.href.match(new RegExp('[?&]' + name + '=([^?&]+)', 'i')) ? decodeURIComponent(RegExp.$1) : '';
  101. }
  102. //执行搜索
  103. function search(address) {
  104. if (!map) return;
  105. var local = new BMap.LocalSearch(map, {
  106. renderOptions: {
  107. map: map,
  108. autoViewport: true,
  109. selectFirstResult: false
  110. }
  111. });
  112. local.search(address);
  113. }
  114. function setMapListener() {
  115. var timer;
  116. map.addEventListener('moveend', mapListenerHandler);
  117. map.addEventListener('zoomend', mapListenerHandler);
  118. marker.addEventListener('dragend', mapListenerHandler);
  119. function mapListenerHandler() {
  120. var zoom = map.getZoom(),
  121. center = map.getCenter(),
  122. marker = window.marker.point;
  123. iframe.src = iframe.src.replace(new RegExp('([?#&])center=([^?#&]+)', 'i'), '$1center=' + center.lng + ',' + center.lat).replace(new RegExp('([?#&])markers=([^?#&]+)', 'i'), '$1markers=' + marker.lng + ',' + marker.lat).replace(new RegExp('([?#&])zoom=([^?#&]+)', 'i'), '$1zoom=' + zoom);
  124. editor.fireEvent('saveScene');
  125. saveScene(editor);
  126. }
  127. function saveScene() {
  128. if (!timer) {
  129. timer = setTimeout(function () {
  130. editor.fireEvent('savescene');
  131. editor.fireEvent('contentchange');
  132. timer = null;
  133. }, 1000);
  134. }
  135. }
  136. }
  137. function getSelfIframe() {
  138. var iframes = parent.document.getElementsByTagName('iframe');
  139. for (var key in iframes) {
  140. if (iframes[key].contentWindow == window) {
  141. return iframes[key];
  142. }
  143. }
  144. return null;
  145. }
  146. function getEditor() {
  147. try {
  148. var parentNode = iframe.parentNode;
  149. while (parentNode && parentNode.tagName && parentNode.tagName.toLowerCase() != 'body') {
  150. if (parentNode.className && parentNode.className.indexOf('edui-body-container') != -1) {
  151. return UM.getEditor(parentNode.nextSibling.id ? parentNode.nextSibling.id : parentNode.id);
  152. }
  153. parentNode = parentNode.parentNode;
  154. }
  155. } catch (e) {
  156. }
  157. return null;
  158. }
  159. </script>
  160. </body>
  161. </html>