当前位置:   article > 正文

vue项目中如何使用百度地图_vue-百度map

vue-百度map

api 参考  JSAPI类参考

两种使用方式:vue特定的VueBaiduMap /原生js

第一种使用 Vue Baidu Map

网址:Vue Baidu Map

实例如下

  1. <comment>
  2. </comment>
  3. <template>
  4. <div id="Map">
  5. <baidu-map
  6. class="bMap"
  7. :center="mainData.city"
  8. :zoom="mainData.zoom"
  9. @ready="initMap"
  10. :mapStyle="mapStyle"
  11. :scroll-wheel-zoom="true"
  12. ak=""
  13. >
  14. <bm-map-type
  15. :map-types="['BMAP_NORMAL_MAP', 'BMAP_SATELLITE_MAP']"
  16. anchor="BMAP_ANCHOR_TOP_RIGHT"
  17. ></bm-map-type>
  18. <!-- :icon="{url: item.projectAlertState === 0 ? require('../../assets/image/emergency/markImgOutline.png') : item.projectAlertState === 1 ? require('../../assets/image/emergency/markImgOnline.png') : require('../../assets/image/emergency/markImgAlart.png') , size: {width: 58, height: 62}}" -->
  19. <bm-marker
  20. v-for="(item,index) in pointsListTest"
  21. :title="item.name"
  22. :key="'point'+index"
  23. :position="{lng: item.lng, lat: item.lat}"
  24. @click="markClick($event,item)"
  25. ></bm-marker>
  26. <!-- <bm-polyline
  27. v-for="(item,index) in polylinePath"
  28. :key="index"
  29. :path="item"
  30. :strokeColor="index%2==0?'#e6a23c':'#f56c6c'"
  31. :strokeOpacity="1"
  32. :strokeWeight="1.5"
  33. ></bm-polyline> -->
  34. <!-- <bm-info-window
  35. v-for="(item,index) in pointsList"
  36. :key="'modal'+index"
  37. :position="{lng: item.lng, lat: item.lat}"
  38. :offset="{height:-20,width:-3}"
  39. :show="currentPointId==item.id"
  40. @close="currentPointId=''"
  41. >
  42. <div style="padding:10px 0">
  43. <p style="margin:5px 0;margin-bottom:15px;">项目名称:{{item.name}}</p>
  44. <p style="text-align:right">
  45. <span class="btn" @click="showDetail(item.id)">查看详情</span>
  46. </p>
  47. </div>
  48. </bm-info-window>-->
  49. <!-- <bm-boundary
  50. name="广州南沙区"
  51. :strokeWeight="3"
  52. strokeColor="#FFFFFF"
  53. :fillOpacity="0.2"
  54. fillColor="#FFFFFF"
  55. ></bm-boundary>-->
  56. </baidu-map>
  57. <div class="modalContainer" v-if="visible" @click="visible=!visible">
  58. <div class="modal">
  59. <div class="modal-title">
  60. <span>详情</span>
  61. <span class="icon" @click.stop="visible=false">
  62. <i class="el-icon-error"></i>
  63. </span>
  64. </div>
  65. <div class="modal-body">
  66. <table>
  67. <tr>
  68. <td style="width:75px;">项目名称:</td>
  69. <td>{{modalData.name}}</td>
  70. </tr>
  71. </table>
  72. </div>
  73. <div class="modal-footer">
  74. <span class="btn" @click.stop="showDetail(modalData.id)">查看详情</span>
  75. </div>
  76. </div>
  77. </div>
  78. </div>
  79. </template>
  80. <script>
  81. import BaiduMap from "vue-baidu-map/components/map/Map.vue"; //百度地图接入
  82. // import { BmlMarkerClusterer } from "vue-baidu-map";
  83. import { BmMarker } from "vue-baidu-map"; //百度地图 撒点
  84. import { BmBoundary } from "vue-baidu-map"; //屏蔽层
  85. import { BmMapType } from "vue-baidu-map"; //地图类型控件 普通图、卫星图、卫星加路网混合图和三维图
  86. import { BmInfoWindow } from "vue-baidu-map"; //地图类型控件 普通图、卫星图、卫星加路网混合图和三维图
  87. import { BmPolyline } from "vue-baidu-map"; //线段
  88. export default {
  89. name: "Map",
  90. components: {
  91. BaiduMap,
  92. // BmlMarkerClusterer,
  93. BmMarker, //标记点
  94. BmBoundary, //屏蔽层
  95. BmMapType, //地图类型控件
  96. BmInfoWindow, //信息窗体-弹出框
  97. BmPolyline //线段
  98. },
  99. data() {
  100. return {
  101. //地图主题样式
  102. mapStyle: {
  103. // styleJson: [
  104. // {
  105. // featureType: "all",
  106. // elementType: "geometry",
  107. // stylers: {
  108. // hue: "#091143",
  109. // saturation: 100
  110. // }
  111. // },
  112. // {
  113. // featureType: "water",
  114. // elementType: "all",
  115. // stylers: {
  116. // color: "#ffffff"
  117. // }
  118. // }
  119. // ],
  120. style: "midnight"
  121. },
  122. visible: false,
  123. modalData: {
  124. name: "",
  125. id: ""
  126. },
  127. BMap: null,
  128. map: null,
  129. blist: [],
  130. districtLoading: 0,
  131. // 项目标点
  132. itemPoints: [],
  133. mainData :{
  134. city: { lng: 113.531, lat: 22.7046 },
  135. zoom: 11
  136. },
  137. currentPointId: "",
  138. pointsList: [],
  139. pointsListTest: [
  140. {
  141. name: "中铁十六局",
  142. id: "936",
  143. lng: "113.3484484793",
  144. lat: "22.845750655",
  145. status: "2",
  146. progress: "1",
  147. type: 5,
  148. zoom: 0,
  149. danger_list: [
  150. { color: "#ff0000", count: "1" },
  151. { color: "#0080ff", count: "4" }
  152. ],
  153. danger_count: 5
  154. },
  155. {
  156. name: "中铁二局",
  157. id: "936",
  158. lng: "113.5602469939",
  159. lat: "22.8256307025",
  160. status: "2",
  161. progress: "1",
  162. type: 5,
  163. zoom: 0,
  164. danger_list: [
  165. { color: "#ff0000", count: "1" },
  166. { color: "#0080ff", count: "4" }
  167. ],
  168. danger_count: 5
  169. },
  170. {
  171. name: "中水利十四局",
  172. id: "936",
  173. lng: "113.403522605",
  174. lat: "22.8079118286",
  175. status: "2",
  176. progress: "1",
  177. type: 5,
  178. zoom: 0,
  179. danger_list: [
  180. { color: "#ff0000", count: "1" },
  181. { color: "#0080ff", count: "4" }
  182. ],
  183. danger_count: 5
  184. },
  185. {
  186. name: "中铁隧道局",
  187. id: "936",
  188. lng: "113.4938821223",
  189. lat: "22.7813844663",
  190. status: "2",
  191. progress: "1",
  192. type: 5,
  193. zoom: 0,
  194. danger_list: [
  195. { color: "#ff0000", count: "1" },
  196. { color: "#0080ff", count: "4" }
  197. ],
  198. danger_count: 5
  199. },
  200. {
  201. name: "广东水电三局",
  202. id: "936",
  203. lng: "113.6039728703",
  204. lat: "22.659178119",
  205. status: "2",
  206. progress: "1",
  207. type: 5,
  208. zoom: 0,
  209. danger_list: [
  210. { color: "#ff0000", count: "1" },
  211. { color: "#0080ff", count: "4" }
  212. ],
  213. danger_count: 5
  214. },
  215. {
  216. name: "广东水电二局",
  217. id: "936",
  218. lng: "113.5582922075",
  219. lat: "22.7981686582",
  220. status: "2",
  221. progress: "1",
  222. type: 5,
  223. zoom: 0,
  224. danger_list: [
  225. { color: "#ff0000", count: "1" },
  226. { color: "#0080ff", count: "4" }
  227. ],
  228. danger_count: 5
  229. }
  230. ],
  231. polylinePath: [
  232. [
  233. { lng: "113.5582922075", lat: "22.7981686582" },
  234. { lng: "113.6039728703", lat: "22.659178119" }
  235. ],
  236. [
  237. { lng: "113.4938821223", lat: "22.7813844663" },
  238. { lng: "113.403522605", lat: "22.8079118286" }
  239. ]
  240. ]
  241. };
  242. },
  243. computed: {},
  244. created() {
  245. },
  246. mounted() {
  247. //this.getOrgInfo();
  248. },
  249. watch: {
  250. // 标记撒点监听
  251. },
  252. methods: {
  253. // 地图init
  254. initMap({ BMap, map }) {
  255. // console.log(BMap, map);
  256. this.BMap = BMap;
  257. this.map = map;
  258. this.blist = [];
  259. //画线
  260. //this.drawLine();
  261. //行政区域增加屏蔽层
  262. this.addDistrict("广州南沙区");
  263. },
  264. //请求后台数据
  265. getOrgInfo() {
  266. //let url = window.location.search;
  267. // if(url){
  268. // sessionStorage.setItem('token',url.substr(1));
  269. // }else{
  270. // let token = sessionStorage.getItem('token');
  271. // if(!token){
  272. // this.$message.error('系统异常,详情请联系管理员');
  273. // return ;
  274. // }
  275. // }
  276. sessionStorage.setItem("token", "f181c937130fdee4ed38a20819e27190");
  277. let aa = new FormData();
  278. aa.set("token", this.$Api.gettoken("token"));
  279. aa.set("level", "5");
  280. this.$Api.post("App/ProjectExtra/get_area_info", aa).then(success => {
  281. if (success.data.code == "0") {
  282. this.pointsList = success.data.data;
  283. //地图相关
  284. } else {
  285. //this.$Api.message('商品获取失败', 'error');
  286. }
  287. });
  288. },
  289. //地图标记点的点击事件
  290. markClick(e, item) {
  291. this.visible = true;
  292. this.modalData = item;
  293. //this.currentPointId = id;
  294. },
  295. // 点击“查看详情”按钮
  296. showDetail(id) {
  297. let _href = window.location.host;
  298. if (_href.indexOf("autobuild") != -1) {
  299. window.open(
  300. "http://autobuild.1357.cn/Admin/SmartScreen/index?access_org_id=" +
  301. id,
  302. "_blank"
  303. );
  304. } else if (_href.indexOf("spm") != -1) {
  305. window.open(
  306. "http://spm.1357.cn/Admin/SmartScreen/index?access_org_id=" + id,
  307. "_blank"
  308. );
  309. }
  310. },
  311. drawLine() {
  312. var sy = new this.BMap.Symbol(BMap_Symbol_SHAPE_BACKWARD_OPEN_ARROW, {
  313. scale: 0.4, //图标缩放大小
  314. strokeColor: "#fff", //设置矢量图标的线填充颜色
  315. strokeWeight: "1.5" //设置线宽
  316. });
  317. var icons = new this.BMap.IconSequence(sy, "10", "30");
  318. // 创建polyline对象
  319. var pois = [
  320. new this.BMap.Point(113.5582922075, 22.7981686582),
  321. new this.BMap.Point(113.6039728703, 22.659178119)
  322. ];
  323. var polyline = new this.BMap.Polyline(pois, {
  324. enableEditing: false, //是否启用线编辑,默认为false
  325. enableClicking: true, //是否响应点击事件,默认为true
  326. icons: [icons],
  327. strokeWeight: "2", //折线的宽度,以像素为单位
  328. strokeOpacity: 1, //折线的透明度,取值范围0 - 1
  329. strokeColor: "#67c23a" //折线颜色
  330. });
  331. this.map.addOverlay(polyline);
  332. },
  333. /**
  334. * 以下方法全都是为了给“广州南沙区增加屏蔽层”
  335. */
  336. addDistrict(districtName) {
  337. //使用计数器来控制加载过程
  338. this.districtLoading++;
  339. var bdary = new this.BMap.Boundary();
  340. bdary.get(districtName, rs => {
  341. //获取行政区域
  342. var count = rs.boundaries.length; //行政区域的点有多少个
  343. if (count === 0) {
  344. alert("未能获取当前输入行政区域");
  345. return;
  346. }
  347. for (let i = 0; i < count; i++) {
  348. this.blist.push({ points: rs.boundaries[i], name: districtName });
  349. }
  350. //加载完成区域点后计数器-1
  351. this.districtLoading--;
  352. if (this.districtLoading == 0) {
  353. //全加载完成后画端点
  354. this.drawBoundary();
  355. }
  356. });
  357. },
  358. //反选行政区
  359. drawBoundary() {
  360. //包含所有区域的点数组
  361. var pointArray = [];
  362. /*画遮蔽层的相关方法
  363. *思路: 首先在中国地图最外画一圈,圈住理论上所有的中国领土,然后再将每个闭合区域合并进来,并全部连到西北角。
  364. * 这样就做出了一个经过多次西北角的闭合多边形*/
  365. //定义中国东南西北端点,作为第一层
  366. var pNW = { lat: 59.0, lng: 73.0 };
  367. var pNE = { lat: 59.0, lng: 136.0 };
  368. var pSE = { lat: 3.0, lng: 136.0 };
  369. var pSW = { lat: 3.0, lng: 73.0 };
  370. //向数组中添加一次闭合多边形,并将西北角再加一次作为之后画闭合区域的起点
  371. var pArray = [];
  372. pArray.push(pNW);
  373. pArray.push(pSW);
  374. pArray.push(pSE);
  375. pArray.push(pNE);
  376. pArray.push(pNW);
  377. //循环添加各闭合区域
  378. for (var i = 0; i < this.blist.length; i++) {
  379. //添加显示用标签层
  380. var label = new this.BMap.Label(this.blist[i].name, {
  381. offset: new this.BMap.Size(20, -10)
  382. });
  383. label.hide();
  384. this.map.addOverlay(label);
  385. //添加多边形层并显示
  386. var ply = new this.BMap.Polygon(this.blist[i].points, {
  387. strokeWeight: 4,
  388. strokeColor: "#ffffff",
  389. fillOpacity: 0.01,
  390. fillColor: " #FFFFFF"
  391. }); //建立多边形覆盖物
  392. // ply.name = this.blist[i].name;
  393. // ply.label = label;
  394. this.map.addOverlay(ply);
  395. //添加名称标签层
  396. // var centerlabel = new this.Label(this.blist[i].name, { offset: new this.BMap.Size(0, 0) });
  397. // centerlabel.setPosition(ply.getBounds().getCenter());
  398. // this.map.addOverlay(centerlabel);
  399. //将点增加到视野范围内
  400. var path = ply.getPath();
  401. pointArray = pointArray.concat(path);
  402. //将闭合区域加到遮蔽层上,每次添加完后要再加一次西北角作为下次添加的起点和最后一次的终点
  403. pArray = pArray.concat(path);
  404. pArray.push(pArray[0]);
  405. }
  406. //限定显示区域,需要引用api库
  407. // var boundply = new this.BMap.Polygon(pointArray);
  408. // BMapLib.AreaRestriction.setBounds(map, boundply.getBounds());
  409. // this.map.setViewport(pointArray); //调整视野
  410. //添加遮蔽层
  411. var plyall = new this.BMap.Polygon(pArray, {
  412. strokeOpacity: 0.0000001,
  413. strokeColor: "#000000",
  414. strokeWeight: 0.00001,
  415. fillColor: "rgba(26, 35, 42, 1)",
  416. fillOpacity: 0.5
  417. }); //建立多边形覆盖物
  418. this.map.addOverlay(plyall);
  419. }
  420. },
  421. destroyed() {}
  422. };
  423. </script>
  424. <style lang="scss">
  425. #Map {
  426. height: 100%;
  427. position: relative;
  428. .anchorBL {
  429. display: none;
  430. }
  431. .modalContainer {
  432. position: absolute;
  433. top: 0;
  434. left: 0;
  435. width: 100%;
  436. height: 100%;
  437. background: rgba(0, 0, 0, 0.12);
  438. .modal {
  439. position: absolute;
  440. width: 300px;
  441. height: 150px;
  442. top: 50%;
  443. left: 50%;
  444. transform: translate(-50%, -50%);
  445. background-image: url("../../assets/imgs/modal_bg.png");
  446. background-color: rgba(0, 0, 0, 0.12);
  447. background-repeat: no-repeat;
  448. background-size: 100% 100%;
  449. background-position: center center;
  450. color: rgba(255, 255, 255, 0.8);
  451. table {
  452. tr {
  453. td {
  454. vertical-align: top;
  455. }
  456. }
  457. }
  458. z-index: 10;
  459. .modal-title {
  460. display: flex;
  461. justify-content: space-between;
  462. margin-top: 10px;
  463. padding: 0px 10px;
  464. .icon {
  465. padding: 0px 0 10px 6px;
  466. font-size: 16px;
  467. color: rgba(255, 255, 255, 0.8);
  468. cursor: pointer;
  469. }
  470. }
  471. .modal-body {
  472. padding: 5px 15px;
  473. height: 54px;
  474. }
  475. .modal-footer {
  476. padding: 5px 15px;
  477. text-align: right;
  478. }
  479. }
  480. }
  481. .bMap {
  482. width: 100%;
  483. height: 100%;
  484. }
  485. .BMap_pop {
  486. div {
  487. }
  488. }
  489. .BMap_pop div,
  490. .BMap_pop .BMap_center,
  491. .BMap_pop .BMap_top,
  492. .BMap_pop .BMap_bottom {
  493. border: none !important;
  494. }
  495. .btn {
  496. cursor: pointer;
  497. padding: 6px 10px;
  498. background: transparent;
  499. text-align: center;
  500. color: #fff;
  501. background: rgba(28, 217, 255, 1);
  502. border-radius: 5px;
  503. // box-shadow: 0px 0px 30px rgba(28, 217, 255, 0.8) inset;
  504. }
  505. }
  506. </style>

第二种:原生js

网址:地图JS API示例 | 百度地图开放平台

实例如下

  1. <template>
  2. <!--需求描述-->
  3. <!--公司级:南沙水务局-->
  4. <!--需求:参考成都倍特项目,公司级大屏地图模式下,选用广州市南沙区地图,且该地图可链接到任意一款地图中的卫星地图中,地图可进行放大缩小,地图上标定点位可以跳转到相应项目的大屏。-->
  5. <!--完成时间:9月29日-->
  6. <div id="btMap">
  7. <div id="map_main" style="height:100%;width:100%;transform: scale(1);margin-left:0px;"></div>
  8. </div>
  9. </template>
  10. <script>
  11. var BMapLib = (window.BMapLib = BMapLib || {});
  12. var blist = [];
  13. var districtLoading = 0;
  14. window.loadJScript = function() {
  15. var script = document.createElement("script");
  16. script.type = "text/javascript";
  17. script.src =
  18. "http://api.map.baidu.com/api?v=2.0&ak=xfpEeKgg9wUnwyVAAzBEL1GRZROmAB32&callback=init";
  19. script.id = "mapsrc";
  20. document.body.appendChild(script);
  21. };
  22. window.showInfo = function(id) {
  23. window.open(
  24. window.location.host + "/Admin/SmartScreen/index?access_org_id=" + id,
  25. "_blank"
  26. );
  27. };
  28. export default {
  29. name: "map",
  30. data() {
  31. return {
  32. soft: [
  33. {
  34. name: "中铁十六局",
  35. id: "936",
  36. lng: "113.3484484793",
  37. lat: "22.845750655",
  38. status: "2",
  39. progress: "1",
  40. type: 5,
  41. zoom: 0,
  42. danger_list: [
  43. { color: "#ff0000", count: "1" },
  44. { color: "#0080ff", count: "4" }
  45. ],
  46. danger_count: 5
  47. },
  48. {
  49. name: "中铁二局",
  50. id: "936",
  51. lng: "113.5602469939",
  52. lat: "22.8256307025",
  53. status: "2",
  54. progress: "1",
  55. type: 5,
  56. zoom: 0,
  57. danger_list: [
  58. { color: "#ff0000", count: "1" },
  59. { color: "#0080ff", count: "4" }
  60. ],
  61. danger_count: 5
  62. },
  63. {
  64. name: "中水利十四局",
  65. id: "936",
  66. lng: "113.403522605",
  67. lat: "22.8079118286",
  68. status: "2",
  69. progress: "1",
  70. type: 5,
  71. zoom: 0,
  72. danger_list: [
  73. { color: "#ff0000", count: "1" },
  74. { color: "#0080ff", count: "4" }
  75. ],
  76. danger_count: 5
  77. },
  78. {
  79. name: "中铁隧道局",
  80. id: "936",
  81. lng: "113.4938821223",
  82. lat: "22.7813844663",
  83. status: "2",
  84. progress: "1",
  85. type: 5,
  86. zoom: 0,
  87. danger_list: [
  88. { color: "#ff0000", count: "1" },
  89. { color: "#0080ff", count: "4" }
  90. ],
  91. danger_count: 5
  92. },
  93. {
  94. name: "广东水电三局",
  95. id: "936",
  96. lng: "113.6039728703",
  97. lat: "22.659178119",
  98. status: "2",
  99. progress: "1",
  100. type: 5,
  101. zoom: 0,
  102. danger_list: [
  103. { color: "#ff0000", count: "1" },
  104. { color: "#0080ff", count: "4" }
  105. ],
  106. danger_count: 5
  107. },
  108. {
  109. name: "广东水电二局",
  110. id: "936",
  111. lng: "113.5582922075",
  112. lat: "22.7981686582",
  113. status: "2",
  114. progress: "1",
  115. type: 5,
  116. zoom: 0,
  117. danger_list: [
  118. { color: "#ff0000", count: "1" },
  119. { color: "#0080ff", count: "4" }
  120. ],
  121. danger_count: 5
  122. }
  123. ]
  124. };
  125. },
  126. methods: {
  127. gdApi(allpoData) {
  128. let that = this;
  129. this.innerVisible = true;
  130. var map = "";
  131. window.init = function() {
  132. map = new BMap.Map("map_main", { enableMapClick: false });
  133. // 创建Map实例
  134. var pointer = new BMap.Point(113.531, 22.7046); //103.829313,36.073536
  135. // var pointer = new BMap.Point(113.311118,22.481607)
  136. that.lat = 113.311118;
  137. that.lng = 22.481607;
  138. map.centerAndZoom(pointer, 11);
  139. map.enableScrollWheelZoom();
  140. map.addControl(
  141. new BMap.MapTypeControl({
  142. mapTypes: [BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP]
  143. })
  144. );
  145. var plength = allpoData.length;
  146. var allpoint = [];
  147. var pstatus = ["未开工", "在建", "停工", "完工"];
  148. var pjindu = ["无", "工期正常", "工期超前", "工期滞后"];
  149. for (let i = plength; i--; ) {
  150. let pointer1 = new BMap.Point(
  151. parseFloat(allpoData[i].lng).toFixed(6),
  152. parseFloat(allpoData[i].lat).toFixed(6)
  153. );
  154. let marker = new BMap.Marker(pointer1);
  155. map.addOverlay(marker);
  156. allpoint.push(pointer1);
  157. addInfo(
  158. `
  159. <div class='dotext' style='margin-top:20px;'>项目名称:${allpoData[i].name}</div><div style='text-align: right;margin-right: 14px'>
  160. <button style="background: #2E5893;color: #fff;border-radius: 4px;font-size: 13px;padding:2px 4px"
  161. onclick="showInfo(${allpoData[i].id})">查看详情
  162. </button></div>
  163. `,
  164. marker
  165. );
  166. }
  167. if (plength <= 0) {
  168. var marker = new BMap.Marker(pointer);
  169. map.addOverlay(marker);
  170. allpoint.push(pointer);
  171. }
  172. // map.disableDragging();
  173. map.disableDoubleClickZoom();
  174. //赋值经纬度
  175. // that.lat = 113.3110;
  176. // that.lng = 22.4816;
  177. var size = new BMap.Size(10, 20);
  178. var mapStyle = { style: "midnight" };
  179. map.setMapStyle(mapStyle);
  180. setTimeout(function() {
  181. getBoundary();
  182. }, 0);
  183. };
  184. function getBoundary() {
  185. blist = [];
  186. addDistrict("广州南沙区");
  187. }
  188. function addDistrict(districtName) {
  189. //使用计数器来控制加载过程
  190. districtLoading++;
  191. var bdary = new BMap.Boundary();
  192. bdary.get(districtName, function(rs) {
  193. //获取行政区域
  194. var count = rs.boundaries.length; //行政区域的点有多少个
  195. if (count === 0) {
  196. alert("未能获取当前输入行政区域");
  197. return;
  198. }
  199. for (var i = 0; i < count; i++) {
  200. blist.push({ points: rs.boundaries[i], name: districtName });
  201. }
  202. //加载完成区域点后计数器-1
  203. districtLoading--;
  204. if (districtLoading == 0) {
  205. //全加载完成后画端点
  206. drawBoundary();
  207. }
  208. });
  209. }
  210. function drawBoundary() {
  211. //包含所有区域的点数组
  212. var pointArray = [];
  213. /*画遮蔽层的相关方法
  214. *思路: 首先在中国地图最外画一圈,圈住理论上所有的中国领土,然后再将每个闭合区域合并进来,并全部连到西北角。
  215. * 这样就做出了一个经过多次西北角的闭合多边形*/
  216. //定义中国东南西北端点,作为第一层
  217. var pNW = { lat: 59.0, lng: 73.0 };
  218. var pNE = { lat: 59.0, lng: 136.0 };
  219. var pSE = { lat: 3.0, lng: 136.0 };
  220. var pSW = { lat: 3.0, lng: 73.0 };
  221. //向数组中添加一次闭合多边形,并将西北角再加一次作为之后画闭合区域的起点
  222. var pArray = [];
  223. pArray.push(pNW);
  224. pArray.push(pSW);
  225. pArray.push(pSE);
  226. pArray.push(pNE);
  227. pArray.push(pNW);
  228. //循环添加各闭合区域
  229. for (var i = 0; i < blist.length; i++) {
  230. //添加显示用标签层
  231. var label = new BMap.Label(blist[i].name, {
  232. offset: new BMap.Size(20, -10)
  233. });
  234. label.hide();
  235. map.addOverlay(label);
  236. //添加多边形层并显示
  237. var ply = new BMap.Polygon(blist[i].points, {
  238. strokeWeight: 4,
  239. strokeColor: "#ffffff",
  240. fillOpacity: 0.01,
  241. fillColor: " #FFFFFF"
  242. }); //建立多边形覆盖物
  243. // ply.name = blist[i].name;
  244. // ply.label = label;
  245. map.addOverlay(ply);
  246. //添加名称标签层
  247. // var centerlabel = new BMap.Label(blist[i].name, { offset: new BMap.Size(0, 0) });
  248. // centerlabel.setPosition(ply.getBounds().getCenter());
  249. // map.addOverlay(centerlabel);
  250. //将点增加到视野范围内
  251. var path = ply.getPath();
  252. pointArray = pointArray.concat(path);
  253. //将闭合区域加到遮蔽层上,每次添加完后要再加一次西北角作为下次添加的起点和最后一次的终点
  254. pArray = pArray.concat(path);
  255. pArray.push(pArray[0]);
  256. }
  257. //限定显示区域,需要引用api库
  258. // var boundply = new BMap.Polygon(pointArray);
  259. // BMapLib.AreaRestriction.setBounds(map, boundply.getBounds());
  260. // map.setViewport(pointArray); //调整视野
  261. //添加遮蔽层
  262. var plyall = new BMap.Polygon(pArray, {
  263. strokeOpacity: 0.0000001,
  264. strokeColor: "#000000",
  265. strokeWeight: 0.00001,
  266. fillColor: "rgba(26, 35, 42, 1)",
  267. fillOpacity: 0.5
  268. }); //建立多边形覆盖物
  269. map.addOverlay(plyall);
  270. }
  271. function addInfo(txt, marker) {
  272. var infoWindow = new BMap.InfoWindow(txt);
  273. infoWindow.id = "newwindow";
  274. marker.addEventListener("click", function() {
  275. this.openInfoWindow(infoWindow);
  276. setTimeout(function() {
  277. let ele = document.querySelector(
  278. '.BMap_pop img[src="http://api0.map.bdimg.com/images/iw_close1d3.gif"]'
  279. );
  280. // console.log(ele.offsetLeft -15);
  281. ele.style.left = ele.offsetLeft - 10 + "px";
  282. }, 0);
  283. });
  284. }
  285. loadJScript();
  286. },
  287. getOrgInfo() {
  288. let url = window.location.search;
  289. if (url) {
  290. sessionStorage.setItem("token", url.substr(1));
  291. } else {
  292. let token = sessionStorage.getItem("token");
  293. if (!token) {
  294. this.$message.error("系统异常,详情请联系管理员");
  295. return;
  296. }
  297. }
  298. let that = this;
  299. let aa = new FormData();
  300. aa.set("token", this.$Api.gettoken("token"));
  301. aa.set("level", "5");
  302. this.$Api.post("App/ProjectExtra/get_area_info", aa).then(success => {
  303. if (success.data.code == "0") {
  304. that.bigmapData = success.data.data;
  305. //地图相关
  306. that.gdApi(that.bigmapData);
  307. } else {
  308. //that.$Api.message('商品获取失败', 'error');
  309. }
  310. });
  311. }
  312. },
  313. mounted() {
  314. // this.gdApi(this.soft);
  315. this.getOrgInfo();
  316. },
  317. created() {}
  318. };
  319. </script>
  320. <style scoped>
  321. #btMap {
  322. /*width: 740px;*/
  323. /*height: 630px;*/
  324. /*overflow: hidden;*/
  325. width: 100%;
  326. height: 100%;
  327. background: rgba(26, 35, 42, 1);
  328. }
  329. button {
  330. border-radius: 2px;
  331. background: #2e5893;
  332. color: #fff;
  333. font-size: 14px;
  334. }
  335. </style>
  336. <style>
  337. .anchorBL {
  338. display: none !important;
  339. }
  340. /*地图标题*/
  341. .BMap_bubble_title {
  342. color: white;
  343. font-size: 13px;
  344. font-weight: bold;
  345. text-align: left;
  346. padding-left: 5px;
  347. padding-top: 5px;
  348. border-bottom: 1px solid gray;
  349. background-color: #2756ca;
  350. }
  351. /* 消息内容 */
  352. .BMap_bubble_content {
  353. background-color: #fff;
  354. padding-left: 5px;
  355. padding-top: 5px;
  356. padding-bottom: 10px;
  357. }
  358. /* 内容 */
  359. .BMap_pop div:nth-child(9) {
  360. top: 35px !important;
  361. border-radius: 7px;
  362. }
  363. /* 左上角删除按键 */
  364. .BMap_pop img {
  365. top: 43px !important;
  366. -left: 215px !important;
  367. }
  368. .BMap_top {
  369. display: none;
  370. }
  371. .BMap_bottom {
  372. display: none;
  373. }
  374. .BMap_center {
  375. display: none;
  376. }
  377. /* 隐藏边角 */
  378. .BMap_pop div:nth-child(1) div {
  379. display: none;
  380. }
  381. .BMap_pop div:nth-child(3) {
  382. display: none;
  383. }
  384. .BMap_pop div:nth-child(5) {
  385. display: none;
  386. }
  387. .BMap_pop div:nth-child(7) {
  388. display: none;
  389. }
  390. .dotext {
  391. width: 108%;
  392. overflow: hidden;
  393. text-overflow: ellipsis;
  394. white-space: nowrap;
  395. font-size: 12px;
  396. line-height: 1.8em;
  397. padding: 0 5px;
  398. display: block !important;
  399. box-sizing: border-box;
  400. }
  401. </style>

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

闽ICP备14008679号