show.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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"
  18. src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script>
  19. </head>
  20. <body onload="initMap();">
  21. <!--百度地图容器-->
  22. <div style="width: 697px; height: 550px; border: #ccc solid 1px;"
  23. id="dituContent"></div>
  24. </body>
  25. <script type="text/javascript">
  26. function getParam(name) {
  27. return location.href.match(new RegExp('[?#&]' + name + '=([^?#&]+)', 'i')) ? RegExp.$1 : '';
  28. }
  29. var map, marker;
  30. var centerParam = getParam('center');
  31. var zoomParam = getParam('zoom');
  32. var widthParam = getParam('width');
  33. var heightParam = getParam('height');
  34. var markersParam = getParam('markers');
  35. var markerStylesParam = getParam('markerStyles');
  36. //创建和初始化地图函数:
  37. function initMap() {
  38. // [FF]切换模式后报错
  39. if (!window.BMap) {
  40. return;
  41. }
  42. var dituContent = document.getElementById('dituContent');
  43. dituContent.style.width = widthParam + 'px';
  44. dituContent.style.height = heightParam + 'px';
  45. createMap();//创建地图
  46. setMapEvent();//设置地图事件
  47. addMapControl();//向地图添加控件
  48. // 创建标注
  49. var markersArr = markersParam.split(',');
  50. var point = new BMap.Point(markersArr[0], markersArr[1]);
  51. marker = new BMap.Marker(point);
  52. marker.enableDragging();
  53. map.addOverlay(marker); // 将标注添加到地图中
  54. if(parent.editor && parent.document.body.contentEditable=="true") { //在编辑状态下
  55. setMapListener();//地图改变修改外层的iframe标签src属性
  56. }
  57. }
  58. //创建地图函数:
  59. function createMap() {
  60. map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图
  61. var centerArr = centerParam.split(',');
  62. var point = new BMap.Point(parseFloat(centerArr[0]), parseFloat(centerArr[1]));//定义一个中心点坐标
  63. map.centerAndZoom(point, parseInt(zoomParam));//设定地图的中心点和坐标并将地图显示在地图容器中
  64. }
  65. //地图事件设置函数:
  66. function setMapEvent() {
  67. map.enableDragging();//启用地图拖拽事件,默认启用(可不写)
  68. map.enableScrollWheelZoom();//启用地图滚轮放大缩小
  69. map.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写)
  70. map.enableKeyboard();//启用键盘上下左右键移动地图
  71. }
  72. //地图控件添加函数:
  73. function addMapControl() {
  74. //向地图中添加缩放控件
  75. var ctrl_nav = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_LARGE});
  76. map.addControl(ctrl_nav);
  77. //向地图中添加缩略图控件
  78. var ctrl_ove = new BMap.OverviewMapControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: 1});
  79. map.addControl(ctrl_ove);
  80. //向地图中添加比例尺控件
  81. var ctrl_sca = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});
  82. map.addControl(ctrl_sca);
  83. }
  84. function setMapListener() {
  85. var editor = parent.editor, containerIframe,
  86. iframes = parent.document.getElementsByTagName('iframe');
  87. for (var key in iframes) {
  88. if (iframes[key].contentWindow == window) {
  89. containerIframe = iframes[key];
  90. break;
  91. }
  92. }
  93. if (containerIframe) {
  94. map.addEventListener('moveend', mapListenerHandler);
  95. map.addEventListener('zoomend', mapListenerHandler);
  96. marker.addEventListener('dragend', mapListenerHandler);
  97. }
  98. function mapListenerHandler() {
  99. var zoom = map.getZoom(),
  100. center = map.getCenter(),
  101. marker = window.marker.getPoint();
  102. containerIframe.src = containerIframe.src.
  103. replace(new RegExp('([?#&])center=([^?#&]+)', 'i'), '$1center=' + center.lng + ',' + center.lat).
  104. replace(new RegExp('([?#&])markers=([^?#&]+)', 'i'), '$1markers=' + marker.lng + ',' + marker.lat).
  105. replace(new RegExp('([?#&])zoom=([^?#&]+)', 'i'), '$1zoom=' + zoom);
  106. editor.fireEvent('saveScene');
  107. }
  108. }
  109. </script>
  110. </html>