show.html 4.8 KB

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