map.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
  5. <!-- <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"> -->
  6. <!--引用百度地图API-->
  7. <style type="text/css">
  8. html,
  9. body {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. .iw_poi_title {
  14. color: #CC5522;
  15. font-size: 14px;
  16. font-weight: bold;
  17. overflow: hidden;
  18. padding-right: 13px;
  19. white-space: nowrap
  20. }
  21. .iw_poi_content {
  22. font: 12px arial, sans-serif;
  23. overflow: visible;
  24. padding-top: 4px;
  25. white-space: -moz-pre-wrap;
  26. word-wrap: break-word
  27. }
  28. </style>
  29. <script type="text/javascript" src="//api.map.baidu.com/api?key=&v=1.1&services=true"></script>
  30. </head>
  31. <body>
  32. <!--百度地图容器-->
  33. <div style="width:500px;height:350px;border:#ccc solid 1px;" id="dituContent"></div>
  34. </body>
  35. <script type="text/javascript">
  36. //创建和初始化地图函数:
  37. function initMap() {
  38. createMap(); //创建地图
  39. setMapEvent(); //设置地图事件
  40. addMapControl(); //向地图添加控件
  41. addMarker(); //向地图中添加marker
  42. }
  43. //创建地图函数:
  44. function createMap() {
  45. var map = new BMap.Map("dituContent"); //在百度地图容器中创建一个地图
  46. var point = new BMap.Point(117.607614, 26.238521); //定义一个中心点坐标
  47. map.centerAndZoom(point, 18); //设定地图的中心点和坐标并将地图显示在地图容器中
  48. window.map = map; //将map变量存储在全局
  49. }
  50. //地图事件设置函数:
  51. function setMapEvent() {
  52. map.enableDragging(); //启用地图拖拽事件,默认启用(可不写)
  53. map.enableScrollWheelZoom(); //启用地图滚轮放大缩小
  54. map.enableDoubleClickZoom(); //启用鼠标双击放大,默认启用(可不写)
  55. map.enableKeyboard(); //启用键盘上下左右键移动地图
  56. }
  57. //地图控件添加函数:
  58. function addMapControl() {
  59. //向地图中添加缩放控件
  60. var ctrl_nav = new BMap.NavigationControl({
  61. anchor: BMAP_ANCHOR_TOP_LEFT,
  62. type: BMAP_NAVIGATION_CONTROL_LARGE
  63. });
  64. map.addControl(ctrl_nav);
  65. //向地图中添加缩略图控件
  66. var ctrl_ove = new BMap.OverviewMapControl({
  67. anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
  68. isOpen: 1
  69. });
  70. map.addControl(ctrl_ove);
  71. //向地图中添加比例尺控件
  72. var ctrl_sca = new BMap.ScaleControl({
  73. anchor: BMAP_ANCHOR_BOTTOM_LEFT
  74. });
  75. map.addControl(ctrl_sca);
  76. }
  77. //标注点数组
  78. var markerArr = [{
  79. title: "宁德市东侨区天湖东路18号市体育中心游泳馆二楼",
  80. content: "",
  81. point: "119.556727|26.6646",
  82. isOpen: 1,
  83. icon: {
  84. w: 35,
  85. h: 35,
  86. l: 0,
  87. t: 0,
  88. x: 0,
  89. lb: 0
  90. }
  91. }];
  92. //创建marker
  93. function addMarker() {
  94. for (var i = 0; i < markerArr.length; i++) {
  95. var json = markerArr[i];
  96. var p0 = json.point.split("|")[0];
  97. var p1 = json.point.split("|")[1];
  98. var point = new BMap.Point(p0, p1);
  99. var iconImg = createIcon(json.icon);
  100. var marker = new BMap.Marker(point, {
  101. icon: iconImg
  102. });
  103. var iw = createInfoWindow(i);
  104. var label = new BMap.Label(json.title, {
  105. "offset": new BMap.Size(json.icon.lb - json.icon.x + 10, -20)
  106. });
  107. marker.setLabel(label);
  108. map.addOverlay(marker);
  109. label.setStyle({
  110. borderColor: "#808080",
  111. color: "#333",
  112. cursor: "pointer"
  113. });
  114. (function() {
  115. var index = i;
  116. var _iw = createInfoWindow(i);
  117. var _marker = marker;
  118. _marker.addEventListener("click", function() {
  119. this.openInfoWindow(_iw);
  120. });
  121. _iw.addEventListener("open", function() {
  122. _marker.getLabel().hide();
  123. })
  124. _iw.addEventListener("close", function() {
  125. _marker.getLabel().show();
  126. })
  127. label.addEventListener("click", function() {
  128. _marker.openInfoWindow(_iw);
  129. })
  130. if (!!json.isOpen) {
  131. label.hide();
  132. _marker.openInfoWindow(_iw);
  133. }
  134. })()
  135. }
  136. }
  137. //创建InfoWindow
  138. function createInfoWindow(i) {
  139. var json = markerArr[i];
  140. var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b>");
  141. return iw;
  142. }
  143. //创建一个Icon
  144. function createIcon(json) {
  145. var icon = new BMap.Icon("/static/img/location.png", new BMap.Size(json.w, json.h), {
  146. imageOffset: new BMap.Size(-json.l, -json.t),
  147. infoWindowOffset: new BMap.Size(json.lb + 5, 1),
  148. offset: new BMap.Size(json.x, json.h)
  149. })
  150. return icon;
  151. }
  152. initMap(); //创建和初始化地图
  153. </script>
  154. </html>