supermap.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <template>
  2. <div v-bind:id="mapDivId" ref="map" style="width: 100%; height: 100%;background: none;"></div>
  3. </template>
  4. <script>
  5. import 'leaflet-side-by-side'
  6. import '@supermap/iclient-leaflet'
  7. import 'leaflet/dist/leaflet.css'
  8. import 'leaflet.markercluster/dist/leaflet.markercluster-src.js';
  9. import 'leaflet.markercluster/dist/MarkerCluster.css';
  10. import 'leaflet.markercluster/dist/MarkerCluster.Default.css';
  11. import 'leaflet-draw';
  12. import 'leaflet-draw/dist/leaflet.draw.css'
  13. import L from 'leaflet'
  14. import {getSuperMap, iconList} from '@/api/components/supermap.js'
  15. // import {setToken} from '../plugins/auth'
  16. export default {
  17. name: "sookaSuperMap",
  18. data() {
  19. return {
  20. mapDivId: "",
  21. map: null,
  22. layers: [],
  23. isAggregationLayers: [],
  24. radiusLayers: [],
  25. myGroup: [],
  26. isAggregationMyGroup: [],
  27. polygon: null,
  28. connectLayer: [],
  29. graphicsLayer: [],
  30. latLngLayers: [],
  31. latLngGroup: [],
  32. editableLayers: [],
  33. }
  34. },
  35. mounted() {
  36. this.initMap();
  37. },
  38. props: {
  39. mapDiv: null,
  40. codes: null,
  41. mapSite: {},
  42. isSideBySide: null,
  43. showLatLng: null,
  44. isEditableLayers: null,
  45. },
  46. methods: {
  47. clearM: async function (isAggregation) {//清理地图标点
  48. if (isAggregation) {
  49. if (this.isAggregationMyGroup != undefined && this.isAggregationMyGroup != false) {
  50. this.isAggregationMyGroup.clearLayers()
  51. }
  52. this.map.removeLayer(this.isAggregationLayers);
  53. this.isAggregationLayers = [];
  54. this.radiusLayers = [];
  55. this.isAggregationLayers = window.L.markerClusterGroup({
  56. //设置为true时显示聚类所占据的范围
  57. showCoverageOnHover: true,
  58. //设置为true时会向低一级聚类缩放
  59. zoomToBoundsOnClick: true,
  60. //增加点位时增加聚合动画(否则会出问题)
  61. animateAddingMarkers: true,
  62. //最大缩放级别点击聚合图标展开图标
  63. spiderfyOnMaxZoom:true,
  64. });
  65. } else {
  66. if (this.myGroup != undefined && this.myGroup != false) {
  67. this.myGroup.clearLayers()
  68. }
  69. this.layers = []
  70. }
  71. },
  72. clearC: async function () {//清理地图画线
  73. if (this.connectLayer != undefined && this.connectLayer != false) {
  74. this.connectLayer.clearLayers();
  75. }
  76. },
  77. clearG: async function () {//清理地图图形
  78. if (this.graphicsLayer != undefined && this.graphicsLayer != false) {
  79. this.graphicsLayer.clearLayers();
  80. }
  81. },
  82. clearP: async function () {//清理点击事件落点
  83. if (this.latLngGroup != undefined && this.latLngGroup != false) {
  84. this.latLngGroup.clearLayers()
  85. }
  86. this.latLngLayers = []
  87. },
  88. initMap: function () {
  89. let _that = this;
  90. this.mapDivId = this.mapDiv;
  91. let mapSite = this.mapSite;
  92. let isSideBySide = this.isSideBySide;
  93. let isEditableLayers = this.isEditableLayers;
  94. // setToken("eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6ImY5Zjg3ZjVmLWQ4NTctNDQxZC04NmQ5LTg4OWExZWRlODE4ZSIsInVzZXJuYW1lIjoiYWRtaW4ifQ.26mAzmaM9pUza9585aLnRMyRd4GxvrWbCxN0erYsuiBDYQiYnyc-TwXVNDI7Xrpt3Bqmbnul-XMszOxYQi12LA");
  95. _that.isAggregationLayers = window.L.markerClusterGroup({
  96. //设置为true时显示聚类所占据的范围
  97. showCoverageOnHover: true,
  98. //设置为true时会向低一级聚类缩放
  99. zoomToBoundsOnClick: true,
  100. //增加点位时增加聚合动画(否则会出问题)
  101. animateAddingMarkers: true,
  102. //最大缩放级别点击聚合图标展开图标
  103. spiderfyOnMaxZoom:true,
  104. });
  105. if (!isSideBySide) { //只有一个地图
  106. if (this.codes.length != 1) {
  107. alert("请输入一个地图code!");
  108. return
  109. }
  110. getSuperMap(this.codes).then(resp => {
  111. let mapList = resp.data;
  112. if (mapList != null && mapList.length > 0) {
  113. let url = mapList[0].url;
  114. let centerLat = mapList[0].centerLat;
  115. let centerLng = mapList[0].centerLng;
  116. let zoom = mapList[0].zoom; //默认比例
  117. let maxZoom = mapList[0].maxZoom; //最大比例
  118. let minZoom = mapList[0].minZoom; //最小比例
  119. let zoomControl = mapList[0].zoomControl == null ? true : mapList[0].zoomControl; //地图缩放+-号是否显示
  120. let attributionControl = mapList[0].attributionControl == null ? true : mapList[0].attributionControl; //logo是否显示
  121. let logoControl = mapList[0].logoControl == null ? false : mapList[0].logoControl; //logo是否显示
  122. let dragging = mapList[0].dragging == null ? true : mapList[0].dragging; //地图是否允许鼠标拖拽
  123. let defaultConfigure = {
  124. crs: window.L.CRS.EPSG4326,
  125. center: [centerLat, centerLng],
  126. maxZoom: maxZoom,
  127. minZoom: minZoom,
  128. zoom: zoom,
  129. editable: true,
  130. zoomControl: zoomControl,
  131. attributionControl: attributionControl,
  132. dragging: dragging,
  133. logoControl: logoControl
  134. };
  135. if (isEditableLayers) {
  136. Object.assign(defaultConfigure, {drawControl: true});
  137. }
  138. let loadConfiguration = Object.assign(defaultConfigure, mapSite);
  139. let mapOption = window.L.map(this.mapDiv, loadConfiguration);
  140. this.map = mapOption;
  141. if (this.showLatLng != undefined) {
  142. let icon = new window.L.Icon({
  143. iconUrl: iconList['marker'],
  144. iconSize: [48, 48],
  145. iconAnchor: [24, 40],
  146. popupAnchor: [-3, -40],
  147. shadowSize: [41, 41],
  148. })
  149. if (isEditableLayers) {
  150. this.editableLayers = new L.FeatureGroup().addTo(mapOption);
  151. let options = {
  152. position: 'topleft',
  153. draw: {
  154. polyline: null,
  155. polygon: {},
  156. circlemarker: null,
  157. circle: null,
  158. rectangle: {},
  159. marker: null,
  160. remove: null
  161. }
  162. };
  163. let drawControl = new window.L.Control.Draw(options);
  164. mapOption.addControl(drawControl);
  165. mapOption.on(window.L.Draw.Event.CREATED, function (e) {
  166. let layer = e.layer;
  167. let layerType = e.layerType;
  168. if (layerType === "polygon") {
  169. let latlng = layer.getLatLngs()[0];
  170. //一个自定义的计算面积的函数
  171. let area = _that.formatArea(latlng);
  172. alert(area);
  173. } else if (layerType === "polyline") {
  174. let latlng = layer.getLatLngs();
  175. //一个自定义的计算长度的函数
  176. let distance = _that.formatLength(latlng);
  177. alert(distance);
  178. }
  179. this.editableLayers.clearLayers();
  180. this.editableLayers.addLayer(layer);
  181. });
  182. }
  183. mapOption.on('dblclick', function (e) {
  184. _that.clearP();
  185. _that.showLatLng(e.latlng.lat, e.latlng.lng);
  186. let marker = window.L.marker([e.latlng.lat, e.latlng.lng], {
  187. icon: icon
  188. }).addTo(mapOption);
  189. _that.latLngLayers.push(marker)
  190. _that.latLngGroup = window.L.layerGroup(_that.latLngLayers)
  191. mapOption.addLayer(_that.latLngGroup)
  192. })
  193. }
  194. window.L.supermap.tiledMapLayer(url).addTo(mapOption);
  195. }
  196. })
  197. } else { //卷帘地图
  198. if (this.codes.length != 2) {
  199. alert("请输入两个地图code!");
  200. return
  201. }
  202. getSuperMap(this.codes).then(resp => {
  203. let mapList = resp.data;
  204. if (mapList != null && mapList.length == 2) {
  205. let stamenurl = mapList[0].url;
  206. let osmurl = mapList[1].url;
  207. let centerLat = mapList[0].centerLat;
  208. let centerLng = mapList[0].centerLng;
  209. let zoom = mapList[0].zoom; //默认比例
  210. let maxZoom = mapList[0].maxZoom; //最大比例
  211. let minZoom = mapList[0].minZoom; //最小比例
  212. let zoomControl = mapList[0].zoomControl == null ? true : mapList[0].zoomControl; //地图缩放+-号是否显示
  213. let attributionControl = mapList[0].attributionControl == null ? true : mapList[0].attributionControl; //logo是否显示
  214. let logoControl = mapList[0].logoControl == null ? false : mapList[0].logoControl; //logo是否显示
  215. let dragging = mapList[0].dragging == null ? true : mapList[0].dragging; //地图是否允许鼠标拖拽
  216. let defaultConfigure = {
  217. crs: window.L.CRS.EPSG4326,
  218. center: [centerLat, centerLng],
  219. maxZoom: maxZoom,
  220. minZoom: minZoom,
  221. zoom: zoom,
  222. editable: true,
  223. zoomControl: zoomControl,
  224. attributionControl: attributionControl,
  225. dragging: dragging,
  226. logoControl: logoControl
  227. };
  228. if (isEditableLayers) {
  229. Object.assign(defaultConfigure, {drawControl: true});
  230. }
  231. let loadConfiguration = Object.assign(defaultConfigure, mapSite);
  232. let mapOption = window.L.map(this.mapDiv, loadConfiguration);
  233. this.map = mapOption;
  234. if (this.showLatLng != undefined) {
  235. let icon = new window.L.Icon({
  236. iconUrl: iconList['marker'],
  237. iconSize: [48, 48],
  238. iconAnchor: [24, 40],
  239. popupAnchor: [-3, -40],
  240. shadowSize: [41, 41],
  241. })
  242. if (isEditableLayers) {
  243. this.editableLayers = new L.FeatureGroup().addTo(mapOption);
  244. let options = {
  245. position: 'topleft',
  246. draw: {
  247. polyline: null,
  248. polygon: {},
  249. circlemarker: null,
  250. circle: null,
  251. rectangle: {},
  252. marker: null,
  253. remove: null
  254. }
  255. };
  256. let drawControl = new window.L.Control.Draw(options);
  257. mapOption.addControl(drawControl);
  258. mapOption.on(window.L.Draw.Event.CREATED, function (e) {
  259. let layer = e.layer;
  260. let layerType = e.layerType;
  261. if (layerType === "polygon") {
  262. let latlng = layer.getLatLngs()[0];
  263. //一个自定义的计算面积的函数
  264. let area = _that.formatArea(latlng);
  265. alert(area);
  266. } else if (layerType === "polyline") {
  267. let latlng = layer.getLatLngs();
  268. //一个自定义的计算长度的函数
  269. let distance = _that.formatLength(latlng);
  270. alert(distance);
  271. }
  272. this.editableLayers.clearLayers();
  273. this.editableLayers.addLayer(layer);
  274. });
  275. }
  276. mapOption.on('dblclick', function (e) {
  277. _that.clearP();
  278. _that.showLatLng(e.latlng.lat, e.latlng.lng);
  279. let marker = window.L.marker([e.latlng.lat, e.latlng.lng], {
  280. icon: icon
  281. }).addTo(mapOption);
  282. _that.latLngLayers.push(marker)
  283. _that.latLngGroup = window.L.layerGroup(_that.latLngLayers)
  284. mapOption.addLayer(_that.latLngGroup)
  285. })
  286. }
  287. let stamenLayer = window.L.supermap.tiledMapLayer(stamenurl).addTo(mapOption);
  288. let osmLayer = window.L.supermap.tiledMapLayer(osmurl).addTo(mapOption);
  289. L.control.sideBySide(stamenLayer, osmLayer).addTo(mapOption);
  290. }
  291. })
  292. }
  293. },
  294. // 获取面积
  295. /**
  296. * polygon [{lat:30,lng:104},{lat:30,lng:104},{lat:30,lng:104}]
  297. */
  298. formatArea: function (polygon) {
  299. //L.GeometryUtil.geodesicArea(),返回number类型的数据,单位是平方米,这里做了一下转化
  300. var seeArea = window.L.GeometryUtil.geodesicArea(polygon);
  301. let area = (seeArea / 10e5).toFixed(2) + "k㎡";
  302. return area;
  303. },
  304. // 获取长度
  305. /**
  306. * line[{lat:30,lng:104},{lat:30,lng:104},{lat:30,lng:104}]
  307. */
  308. formatLength: function (line) {
  309. let dis = 0;
  310. for (let i = 0; i < line.length - 1; i++) {
  311. let start = line[i];
  312. let end = line[i + 1];
  313. dis += window.L.latLng([start.lat, start.lng]).distanceTo([end.lat, end.lng]);//计算两个点之间的距离,并累加
  314. }
  315. //结果得到的也是number类型,单位是 米
  316. return (dis / 10e2).toFixed(2) + "km";
  317. },
  318. setMarkers: function (markersList) {//地图标点
  319. const _that = this;
  320. for (let i = 0; i < markersList.length; i++) {
  321. let isAggregation = markersList[i].isAggregation == null ? false : markersList[i].isAggregation; //是否聚合点位
  322. let keepBindPopup = isAggregation == true ? false : markersList[i].keepBindPopup; //提示气泡是否一直显示
  323. let icon = new window.L.Icon({
  324. iconUrl: iconList[markersList[i].icon],
  325. iconSize: [48, 48],
  326. iconAnchor: [24, 40],
  327. popupAnchor: [-3, -40],
  328. shadowSize: [41, 41],
  329. })
  330. let markerClick = window.L.marker([markersList[i].lat, markersList[i].lng], {
  331. icon: icon
  332. }).addTo(this.map);
  333. if (markersList[i].bindPopupHtml != null && markersList[i].bindPopupHtml !== "") {
  334. let html = markersList[i].bindPopupHtml;
  335. if (keepBindPopup) {
  336. markerClick.bindPopup(html, {
  337. autoClose: false,
  338. closeOnClick: null,
  339. closeButton: false
  340. }).openPopup(markerClick.getLatLng());
  341. } else {
  342. markerClick.on('mouseover', function () {
  343. let html = markersList[i].bindPopupHtml;
  344. this.bindPopup(html).openPopup(this.getLatLng());
  345. }).on('mouseout', function () {
  346. this.closePopup();
  347. })
  348. }
  349. }
  350. if (markersList[i].click != null && markersList[i].click !== "") {
  351. if(markersList[i].parameter != null && markersList[i].parameter !== ""){
  352. markerClick.on('click', function () {
  353. let clickName = markersList[i].click;
  354. _that.$emit(clickName,markersList[i].parameter);
  355. })
  356. }else{
  357. markerClick.on('click', function () {
  358. let clickName = markersList[i].click;
  359. _that.$emit(clickName);
  360. })
  361. }
  362. }
  363. if (isAggregation) {
  364. _that.isAggregationLayers.addLayer(markerClick)
  365. _that.isAggregationMyGroup = window.L.layerGroup(_that.isAggregationLayers)
  366. this.map.addLayer(_that.isAggregationLayers);
  367. } else {
  368. _that.layers.push(markerClick)
  369. _that.myGroup = window.L.layerGroup(_that.layers)
  370. this.map.addLayer(_that.myGroup)
  371. }
  372. }
  373. },
  374. setMarkersRadius: function (markersList) {//地图标点带范围
  375. const _that = this;
  376. for (let i = 0; i < markersList.length; i++) {
  377. let isAggregation = markersList[i].isAggregation == null ? false : markersList[i].isAggregation; //是否聚合点位
  378. let keepBindPopup = isAggregation == true ? false : markersList[i].keepBindPopup; //提示气泡是否一直显示
  379. _that.polygon = window.L.circle([markersList[i].lat, markersList[i].lng], {radius: markersList[i].radius * 1000});
  380. let icon = new window.L.Icon({//传感器
  381. iconUrl: iconList[markersList[i].icon],
  382. iconSize: [48, 48],
  383. iconAnchor: [24, 40],
  384. popupAnchor: [-3, -40],
  385. shadowSize: [41, 41],
  386. })
  387. let marker = window.L.marker([markersList[i].lat, markersList[i].lng], {
  388. icon: icon
  389. });
  390. if (markersList[i].bindPopupHtml != null && markersList[i].bindPopupHtml !== "") {
  391. let html = markersList[i].bindPopupHtml;
  392. if (keepBindPopup) {
  393. marker.bindPopup(html, {
  394. autoClose: false,
  395. closeOnClick: null,
  396. closeButton: false
  397. }).openPopup(marker.getLatLng());
  398. } else {
  399. marker.on('mouseover', function () {
  400. let html = markersList[i].bindPopupHtml;
  401. this.bindPopup(html).openPopup(this.getLatLng());
  402. }).on('mouseout', function () {
  403. this.closePopup();
  404. })
  405. }
  406. }
  407. if (markersList[i].click != null && markersList[i].click != "") {
  408. if(markersList[i].parameter != null && markersList[i].parameter !== ""){
  409. marker.on('click', function () {
  410. let clickName = markersList[i].click;
  411. _that.$emit(clickName,markersList[i].parameter);
  412. })
  413. }else{
  414. marker.on('click', function () {
  415. let clickName = markersList[i].click;
  416. _that.$emit(clickName);
  417. })
  418. }
  419. }
  420. marker.addTo(this.map);
  421. if (isAggregation) {
  422. _that.isAggregationLayers.addLayer(marker);
  423. _that.radiusLayers.push(_that.polygon);
  424. this.map.addLayer(_that.isAggregationLayers);
  425. _that.isAggregationMyGroup = window.L.layerGroup(_that.radiusLayers)
  426. this.map.addLayer(_that.isAggregationMyGroup)
  427. } else {
  428. _that.layers.push(marker);
  429. _that.layers.push(_that.polygon);
  430. _that.myGroup = window.L.layerGroup(_that.layers)
  431. this.map.addLayer(_that.myGroup)
  432. }
  433. }
  434. },
  435. setConnectList: function (connectList, color) {//地图画线
  436. const _that = this;
  437. let points = [];
  438. for (let i = 0; i < connectList.length; i++) {
  439. points.push([connectList[i].lat, connectList[i].lng]);//创建点
  440. }
  441. let polyline = window.L.polyline(points, {color: color});
  442. _that.connectLayer = window.L.featureGroup().addTo(this.map);
  443. _that.connectLayer.addLayer(polyline);
  444. },
  445. setGraphicsList: function (graphicsList, color) {//地图图形
  446. const _that = this;
  447. let points = [];
  448. for (let i = 0; i < graphicsList.length; i++) {
  449. points.push([graphicsList[i].lat, graphicsList[i].lng]);//创建点
  450. }
  451. let polygon = window.L.polygon(points, {color: color});
  452. polygon.addTo(this.map);
  453. _that.graphicsLayer = window.L.featureGroup().addTo(this.map);
  454. _that.graphicsLayer.addLayer(polygon);
  455. },
  456. dropLocation: function (lat, lng) {//落点定位
  457. this.map.panTo([lat, lng])
  458. },
  459. controlLevel: function (level) {//控制地图缩放级别
  460. this.map.setZoom(level)
  461. }
  462. }
  463. }
  464. </script>
  465. <style scoped>
  466. </style>