BMap.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <el-main class="page">
  3. <div class="el-scrollbar__wrap wrap-white">
  4. <div class="el-scrollbar__view">
  5. <!--百度地图容器-->
  6. <div
  7. style="width: 100%; height: 800px; border: #ccc solid 1px"
  8. id="dituContent"
  9. ></div>
  10. <v-dialog :title="title" v-model="dialogTableVisible">
  11. <el-image
  12. style="width: 500px; height: 300px"
  13. src="https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg"
  14. :fit="fit"
  15. />
  16. </v-dialog>
  17. </div>
  18. </div>
  19. </el-main>
  20. </template>
  21. <script>
  22. // 引入富文本编辑器所需样式
  23. import { MP } from "./map.js";
  24. export default {
  25. data() {
  26. return {
  27. ak: "I7ioGmQbypWKuWMNv5fLPKU4mt3gzhrz", // 正式线上项目,务必替换成自己的key
  28. dialogTableVisible: false,
  29. BMap: null,
  30. map: null,
  31. title: "",
  32. imgUrl: "",
  33. index: 1,
  34. markerArr1: [
  35. {
  36. title: "紫金山",
  37. content: "~@/assets/img/zjs.png",
  38. point: "118.853196|32.08811",
  39. isOpen: 0,
  40. icon: { w: 23, h: 25, l: 46, t: 21, x: 9, lb: 12 },
  41. },
  42. {
  43. title: "玄武湖",
  44. content: "~@/assets/img/xwh.png",
  45. point: "118.804616|32.080278",
  46. isOpen: 0,
  47. icon: { w: 23, h: 25, l: 46, t: 21, x: 9, lb: 12 },
  48. },
  49. ],
  50. };
  51. },
  52. mounted() {
  53. let _this = this;
  54. MP(this.ak).then((BMap) => {
  55. // 百度地图API功能
  56. _this.BMap = BMap;
  57. _this.initMap();
  58. });
  59. },
  60. methods: {
  61. initMap() {
  62. // 百度地图API功能
  63. this.createMap(); // 创建地图
  64. this.setMapEvent(); // 设置地图事件
  65. this.addMapControl(); // 向地图添加控件
  66. this.addMarker(this.markerArr1); // 向地图中添加marker
  67. },
  68. createMap() {
  69. var map = new this.BMap.Map("dituContent"); // 在百度地图容器中创建一个地图
  70. var point = new this.BMap.Point(118.853196, 32.08811); // 定义一个中心点坐标
  71. map.centerAndZoom(point, 14); // 设定地图的中心点和坐标并将地图显示在地图容器中
  72. // map.setMapStyleV2({
  73. // styleId: '44ce7d9e957f49e0f17ccee298d7bdd4'
  74. // })
  75. this.map = map; // 将map变量存储在全局
  76. },
  77. setMapEvent() {
  78. this.map.enableDragging(); // 启用地图拖拽事件,默认启用(可不写)
  79. this.map.enableScrollWheelZoom(); // 启用地图滚轮放大缩小
  80. this.map.enableDoubleClickZoom(); // 启用鼠标双击放大,默认启用(可不写)
  81. this.map.enableKeyboard(); // 启用键盘上下左右键移动地图
  82. },
  83. addMapControl() {
  84. // 向地图中添加缩放控件
  85. var ctrlMav = new this.BMap.NavigationControl({
  86. anchor: window.BMAP_ANCHOR_TOP_LEFT,
  87. type: window.BMAP_NAVIGATION_CONTROL_LARGE,
  88. });
  89. this.map.addControl(ctrlMav);
  90. // 向地图中添加缩略图控件
  91. var ctrlOve = new this.BMap.OverviewMapControl({
  92. anchor: window.BMAP_ANCHOR_BOTTOM_RIGHT,
  93. isOpen: 1,
  94. });
  95. this.map.addControl(ctrlOve);
  96. // 向地图中添加比例尺控件
  97. var ctrlSca = new this.BMap.ScaleControl({
  98. anchor: window.BMAP_ANCHOR_BOTTOM_LEFT,
  99. });
  100. this.map.addControl(ctrlSca);
  101. },
  102. addMarker(markerArr) {
  103. let _this = this;
  104. for (var i = 0; i < markerArr.length; i++) {
  105. var json = markerArr[i];
  106. var p0 = json.point.split("|")[0];
  107. var p1 = json.point.split("|")[1];
  108. var point = new this.BMap.Point(p0, p1);
  109. var iconImg = this.createIcon(json.icon);
  110. var marker = new this.BMap.Marker(point, { icon: iconImg });
  111. let jsonx = this.createInfoWindow(i, markerArr);
  112. var label = new this.BMap.Label(json.title, {
  113. offset: new this.BMap.Size(
  114. json.icon.lb - json.icon.x + 10,
  115. -20
  116. ),
  117. });
  118. marker.setLabel(label);
  119. this.map.addOverlay(marker);
  120. label.setStyle({
  121. borderColor: "#808080",
  122. color: "#333",
  123. cursor: "pointer",
  124. });
  125. (function () {
  126. var index = i;
  127. jsonx = _this.createInfoWindow(i, markerArr);
  128. var _marker = marker;
  129. _marker.addEventListener("click", function () {
  130. _this.open(index, jsonx);
  131. });
  132. label.addEventListener("click", function () {
  133. _this.open(index, jsonx);
  134. });
  135. if (json.isOpen) {
  136. label.hide();
  137. }
  138. })();
  139. }
  140. },
  141. createInfoWindow(i, markerArr) {
  142. var json = markerArr[i];
  143. return json;
  144. },
  145. open(index, json) {
  146. this.dialogTableVisible = true;
  147. this.index = index + 1;
  148. this.title = json.title;
  149. this.imgUrl = json.content;
  150. },
  151. createIcon(json) {
  152. var icon = new this.BMap.Icon(
  153. "http://map.baidu.com/image/us_mk_icon.png",
  154. new this.BMap.Size(json.w, json.h),
  155. {
  156. imageOffset: new this.BMap.Size(-json.l, -json.t),
  157. infoWindowOffset: new this.BMap.Size(json.lb + 5, 1),
  158. offset: new this.BMap.Size(json.x, json.h),
  159. }
  160. );
  161. return icon;
  162. },
  163. },
  164. };
  165. </script>
  166. <style lang="scss" scoped>
  167. .article-box {
  168. width: 100%;
  169. .el-pagination {
  170. margin-top: 20px;
  171. }
  172. .quill-editor {
  173. height: 300px;
  174. margin-bottom: 20px;
  175. }
  176. }
  177. </style>