map.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta charset="utf-8"/>
  6. <meta name="keywords" content="百度地图,百度地图API,百度地图自定义工具,百度地图所见即所得工具"/>
  7. <meta name="description" content="百度地图API自定义地图,帮助用户在可视化操作下生成百度地图"/>
  8. <title>百度地图API自定义地图</title>
  9. <!--引用百度地图API-->
  10. <style type="text/css">
  11. html, body {
  12. margin: 0;
  13. padding: 0;
  14. overflow: hidden;
  15. }
  16. </style>
  17. <script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=2.0&ak=6b6c1a67eaa7db1ca6d6da28e590e343&services=true"></script>
  18. </head>
  19. <body onload="initMap();">
  20. <!--百度地图容器-->
  21. <div style="width:697px;height:550px;border:#ccc solid 1px;" id="dituContent"></div>
  22. </body>
  23. <script type="text/javascript">
  24. function getParam(name) {
  25. return location.href.match(new RegExp('[?#&]' + name + '=([^?#&]+)', 'i')) ? RegExp.$1 : '';
  26. }
  27. var map, marker;
  28. var centerParam = getParam('center');
  29. var zoomParam = getParam('zoom');
  30. var widthParam = getParam('width');
  31. var heightParam = getParam('height');
  32. var markersParam = getParam('markers');
  33. var markerStylesParam = getParam('markerStyles');
  34. var iframe = getSelfIframe();
  35. var UM = parent.UM;
  36. var editor = getEditor();
  37. //创建和初始化地图函数:
  38. function initMap() {
  39. // [FF]切换模式后报错
  40. if (!window.BMap) {
  41. return;
  42. }
  43. var dituContent = document.getElementById('dituContent');
  44. dituContent.style.width = widthParam + 'px';
  45. dituContent.style.height = heightParam + 'px';
  46. createMap();//创建地图
  47. setMapEvent();//设置地图事件
  48. addMapControl();//向地图添加控件
  49. // 创建标注
  50. var markersArr = markersParam.split(',');
  51. var point = new BMap.Point(markersArr[0], markersArr[1]);
  52. marker = new BMap.Marker(point);
  53. marker.enableDragging();
  54. map.addOverlay(marker); // 将标注添加到地图中
  55. if(iframe && UM && editor) { //在编辑状态下
  56. setMapListener();//地图改变修改外层的iframe标签src属性
  57. } else {
  58. document.focus();
  59. }
  60. }
  61. //创建地图函数:
  62. function createMap() {
  63. map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图
  64. var centerArr = centerParam.split(',');
  65. var point = new BMap.Point(parseFloat(centerArr[0]), parseFloat(centerArr[1]));//定义一个中心点坐标
  66. map.centerAndZoom(point, parseInt(zoomParam));//设定地图的中心点和坐标并将地图显示在地图容器中
  67. }
  68. //地图事件设置函数:
  69. function setMapEvent() {
  70. map.enableDragging();//启用地图拖拽事件,默认启用(可不写)
  71. map.enableScrollWheelZoom();//启用地图滚轮放大缩小
  72. map.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写)
  73. map.enableKeyboard();//启用键盘上下左右键移动地图
  74. }
  75. //地图控件添加函数:
  76. function addMapControl() {
  77. //向地图中添加缩放控件
  78. var ctrl_nav = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_LARGE});
  79. map.addControl(ctrl_nav);
  80. //向地图中添加缩略图控件
  81. var ctrl_ove = new BMap.OverviewMapControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: 1});
  82. map.addControl(ctrl_ove);
  83. //向地图中添加比例尺控件
  84. var ctrl_sca = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});
  85. map.addControl(ctrl_sca);
  86. }
  87. function setMapListener() {
  88. var timer;
  89. map.addEventListener('moveend', mapListenerHandler);
  90. map.addEventListener('zoomend', mapListenerHandler);
  91. marker.addEventListener('dragend', mapListenerHandler);
  92. function mapListenerHandler() {
  93. var zoom = map.getZoom(),
  94. center = map.getCenter(),
  95. marker = window.marker.P;
  96. iframe.src = iframe.src.
  97. replace(new RegExp('([?#&])center=([^?#&]+)', 'i'), '$1center=' + center.lng + ',' + center.lat).
  98. replace(new RegExp('([?#&])markers=([^?#&]+)', 'i'), '$1markers=' + marker.lng + ',' + marker.lat).
  99. replace(new RegExp('([?#&])zoom=([^?#&]+)', 'i'), '$1zoom=' + zoom);
  100. editor.fireEvent('saveScene');
  101. saveScene(editor);
  102. }
  103. function saveScene(){
  104. if(!timer) {
  105. timer = setTimeout(function(){
  106. editor.fireEvent('savescene');
  107. editor.fireEvent('contentchange');
  108. timer = null;
  109. }, 1000);
  110. }
  111. }
  112. }
  113. function getSelfIframe(){
  114. var iframes = parent.document.getElementsByTagName('iframe');
  115. for (var key in iframes) {
  116. if (iframes[key].contentWindow == window) {
  117. return iframes[key];
  118. }
  119. }
  120. return null;
  121. }
  122. function getEditor(){
  123. var parentNode = iframe.parentNode;
  124. while (parentNode && parentNode.tagName && parentNode.tagName.toLowerCase() != 'body') {
  125. if (parentNode.className && parentNode.className.indexOf('edui-body-container')!=-1) {
  126. return UM.getEditor(parentNode.id);
  127. }
  128. parentNode = parentNode.parentNode;
  129. }
  130. return null;
  131. }
  132. </script>
  133. </html>