7f49088013af24533ee987356617b018c2d85219.svn-base 4.7 KB

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