当前位置:   article > 正文

百度地图3.0自定义mark弹窗样式,多个标注点例子_百度地图.js mark文本展示框样式

百度地图.js mark文本展示框样式

  最终效果

tempalte样式 

  1. <template>
  2.   <div>
  3. <!--地图-->
  4.       <div :id="containerId" class="map-div"></div>
  5.   </div>
  6. </template>

html引入

  1. <script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=xxxxxxx"></script>
  2. <!--自定义点击mark显示弹窗样式需要引入的js-->
  3. <script type="text/javascript" src="http://api.map.baidu.com/library/InfoBox/1.2/src/InfoBox_min.js"></script>

 js

  1. <script>
  2. export default {
  3. name: "profile",
  4. data() {
  5. return {
  6. map: undefined,
  7. markersC: [
  8. {
  9. enableClicking: true,
  10. lng: 116.482529,
  11. lat: 39.923623,
  12. img: require("@/assets/modules/images/bird.png"), //标准店自定义图标
  13. title: "杜鹃",
  14. num: 172,
  15. label: "2023-05-02"
  16. },
  17. {
  18. img: require("@/assets/modules/images/bird.png"),
  19. enableClicking: true,
  20. lng: 116.50222,
  21. lat: 39.920026,
  22. title: "赤鸟",
  23. num: 73,
  24. label: "2023-05-02"
  25. },
  26. {
  27. enableClicking: true,
  28. img: require("@/assets/modules/images/bird.png"),
  29. title: "灰鹤",
  30. lng: 116.476061,
  31. lat: 39.879286,
  32. num: 82,
  33. label: "2023-05-02"
  34. },
  35. {
  36. enableClicking: true,
  37. img: require("@/assets/modules/images/bird.png"),
  38. title: "锦鸟",
  39. lng: 116.476061,
  40. lat: 39.877071,
  41. num: 67,
  42. label: "2023-05-02"
  43. },
  44. {
  45. enableClicking: true,
  46. img: require("@/assets/modules/images/bird.png"),
  47. title: "麻雀",
  48. lng: 116.476061,
  49. lat: 39.919805,
  50. num: 237,
  51. label: "2023-05-02"
  52. }
  53. ],
  54. containerId: "hazard-level-container2"
  55. };
  56. },
  57. mounted() {
  58. this.init();
  59. },
  60. methods: {
  61. openInfo(item, point) {
  62. //弹框右上角关闭图标
  63. let closeicon = require("@/assets/modules/images/close.png");
  64. //弹框样式
  65. let info = `<div style="color:#fff;padding:10px;">
  66. <span style="font-size: 14px;">${item.title}
  67. <span style="color: #F59A23;">:${item.num}</span>
  68. 只</span>
  69. <div style="color:#ccc">${item.label}</div>
  70. </div>`;
  71. let infoBox = new BMapLib.InfoBox(this.map, info, {
  72. boxStyle: {
  73. opacity: "1",
  74. background: "rgba(125,178,205,0.9)",
  75. width: "150px",
  76. height: "50px",
  77. marginBottom: "40px"
  78. },
  79. // closeIconMargin: "18px 8px 4px 4px",
  80. closeIconUrl: closeicon,
  81. enableAutoPan: true
  82. });
  83. //打开弹框
  84. infoBox.open(point);
  85. },
  86. init() {
  87. // 创建地图
  88. this.map = this.initMap(this.containerId);
  89. // 百度地图API功能
  90. this.markers(); //获取标注
  91. },
  92. markers() {
  93. let mar = [];
  94. let iconSize = new BMap.Size(35, 44);
  95. let marker = [];
  96. let point = [];
  97. for (let i in this.markersC) {
  98. point[i] = new BMap.Point(this.markersC[i].lng, this.markersC[i].lat);
  99. let myIcon = new BMap.Icon(this.markersC[i].img, iconSize, {
  100. imageSize: iconSize
  101. });
  102. marker[i] = new BMap.Marker(point[i], {
  103. icon: myIcon,
  104. enableClicking: true
  105. });
  106. this.map.centerAndZoom(point[i], 14);
  107. this.map.addOverlay(marker[i]); // 将标注添加到地图中
  108. marker[i].addEventListener("click", r => {
  109. //打开自定义弹窗
  110. this.openInfo(this.markersC[i], point[i], this.map); //
  111. });
  112. }
  113. },
  114. initMap(containerId, options) {
  115. let map = new BMap.Map(containerId, {
  116. restrictCenter: false,
  117. minZoom: 3,
  118. maxZoom: 21
  119. // mapType: BMAP_EARTH_MAP //必须先设置3D效果再设置地图类型,否则3D效果无效
  120. });
  121. let center;
  122. if (options && options.centerX && options.centerY) {
  123. center = new BMap.Point(options.centerX, options.centerY);
  124. } else {
  125. center = new BMap.Point(116.609873, 40.083128);
  126. }
  127. let level = options && options.level ? options.level : 14;
  128. map.centerAndZoom(center, level);
  129. map.enableKeyboard();
  130. map.enableScrollWheelZoom();
  131. map.enableInertialDragging();
  132. map.enableContinuousZoom();
  133. map.setMapType(BMAP_HYBRID_MAP); //设置地图样式
  134. return map;
  135. }
  136. }
  137. };
  138. </script>

 css

  1. <style scoped>
  2. .map-div {
  3. width: calc(100% - 3.5rem);
  4. height: calc(100vh - 3rem);
  5. overflow: hidden;
  6. margin: 0;
  7. font-family: "微软雅黑", serif;
  8. /*border-radius: 5px;*/
  9. /*background-color: #1a1a1a;*/
  10. }
  11. </style>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/109646
推荐阅读
相关标签
  

闽ICP备14008679号