Map.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <div class="MapViewer" ref="map"></div>
  3. </template>
  4. <script>
  5. import publicFun from "@/utils/publicFunction.js";
  6. export default {
  7. name: "Map",
  8. data() {
  9. return {
  10. map: "",
  11. layers: "",
  12. polygon: [],
  13. measureTool: ""
  14. };
  15. },
  16. mounted() {
  17. //地图初始化
  18. this.$nextTick(() => {
  19. this.mapInit();
  20. let that = this;
  21. this.map.on("move", e => {
  22. if (that.index == that.mouseIndex) {
  23. let center = that.map.getCenter();
  24. let zoom = that.map.getZoom();
  25. this.$emit("changeCenterZoom", {
  26. center: [center.lat, center.lng],
  27. zoom: zoom,
  28. index: that.index
  29. });
  30. }
  31. });
  32. // this.initDraw();
  33. });
  34. },
  35. props: ["mapUrl", "index", "centerZoom", "mouseIndex", "centerZoomInit"],
  36. destroy() {
  37. this.stopLabelCase();
  38. },
  39. methods: {
  40. // 初始化地图标记事件
  41. initDraw() {
  42. var defaultData = {
  43. action: "add",
  44. position: "topleft",
  45. offset: [0, 0],
  46. background: "#fff",
  47. color: "#000",
  48. font_size: "14px",
  49. closeButton: true
  50. };
  51. // 引入疑点标记绘制方法
  52. this.map.on("draw2-result", data => {
  53. let geoType = null;
  54. // 纬经度
  55. if (data && data.points.length >= 1) {
  56. // 类型: 0 点;1 线;2 面;3 矩形;4 圆形;5 其他;
  57. if (data.points.length === 1 && data.area === 0) {
  58. geoType = 0;
  59. }
  60. if (data.points.length === 2 && data.distance > 0) {
  61. geoType = 4;
  62. }
  63. if (data.points.length > 2 && data.area === 0) {
  64. geoType = 1;
  65. }
  66. if (data.points.length > 2 && data.area != 0 && data.area != "") {
  67. geoType = 2;
  68. }
  69. }
  70. // 首先保存数据,然后删除底图上的标记
  71. let coordinates = [data.points];
  72. let polygon = L.polygon(coordinates, {
  73. color: "red",
  74. weight: 3,
  75. fillColor: "red",
  76. opacity: 1,
  77. fillOpacity: 0
  78. }).addTo(this.map);
  79. polygon.on("click", e => {
  80. // polygon.setStyle({
  81. // fillOpacity: 0.35
  82. // });
  83. // if (geometry.geometry && geometry.geometry.properties) {
  84. // this.$store.state.mapMarkInfo = geometry.geometry.properties;
  85. // }
  86. });
  87. this.measureTool._measureGroup && this.measureTool._measureGroup.remove();
  88. // mousemove2Status = false;
  89. // this.map.doubleClickZoom.disable();
  90. // this.map.off("click");
  91. // if (this.measureTool.moveMarker) {
  92. // this.map.removeLayer(this.measureTool.moveMarker);
  93. // this.measureTool.moveMarker = null;
  94. // }
  95. });
  96. if (this.index == 0) {
  97. this.measureTool = new LControlDraw({
  98. position: defaultData.position,
  99. offset: defaultData.offset,
  100. background: defaultData.background,
  101. color: defaultData.color,
  102. font_size: defaultData.font_size,
  103. closeButton: defaultData.closeButton,
  104. iconUrl: "/static/plugins/draw-plugin/images/marker-icon.png"
  105. }).addTo(this.map);
  106. }
  107. },
  108. // 开始标记疑点事件
  109. startLabelCase() {
  110. this.measureTool.start();
  111. },
  112. // 停止标记疑点事件
  113. stopLabelCase() {
  114. this.map.off("draw2-result");
  115. if (this.measureTool) {
  116. // 移除绘制的图形
  117. this.measureTool._measureGroup && this.measureTool._measureGroup.remove();
  118. mousemove2Status = false;
  119. this.map.doubleClickZoom.disable();
  120. this.map.off("click");
  121. if (this.measureTool.moveMarker) {
  122. this.map.removeLayer(this.measureTool.moveMarker);
  123. this.measureTool.moveMarker = null;
  124. }
  125. this.measureTool.remove();
  126. this.measureTool = null;
  127. }
  128. },
  129. addSinglePolygon(state) {
  130. this.$nextTick(() => {
  131. if (!state && this.polygon) {
  132. this.polygon.forEach(item => {
  133. this.map.removeLayer(item);
  134. });
  135. this.addSinglePolygon(true);
  136. } else if (this.$store.state.selectSelectDataMap && this.$store.state.selectSelectDataMap.singlePolygon) {
  137. for (let item in this.$store.state.selectSelectDataMap.singlePolygon) {
  138. // 如果是叠置分析的图层处理方式this.$store.state.selectSelectDataMap.singlePolygon
  139. if (this.$store.state.selectSelectDataMap.singlePolygon[item][0].uniqueId.indexOf("overlay") > -1) {
  140. let uniqueId = this.$store.state.selectSelectDataMap.singlePolygon[item][0].uniqueId;
  141. let color = this.$store.state.selectSelectDataMap.singlePolygon[item][0].color;
  142. let statesArr = this.$store.state.selectSelectDataMap.singlePolygon[item].map(v => {
  143. return JSON.parse(v.geometry);
  144. });
  145. this.readGeojson(statesArr, color);
  146. } else {
  147. // 非叠置分析图层的处理方式
  148. let index = 0;
  149. this.$store.state.selectSelectDataMap.singlePolygon[item].forEach(v => {
  150. index++;
  151. let cid = v.cid + "_tpdb_" + index;
  152. let geometry = v.geometry;
  153. let coord = JSON.parse(geometry).geometry.coordinates;
  154. // 经纬度顺序转换
  155. let coordinates = publicFun.latLngsToReverse(coord);
  156. let polygon = L.polygon(coordinates, {
  157. color: v.color,
  158. weight: 3,
  159. fillColor: v.color,
  160. opacity: 1,
  161. fillOpacity: 0
  162. }).addTo(this.map);
  163. polygon.on("click", e => {
  164. // 鼠标点击图版高亮提示逻辑,暂时不用,如果需要的话就要
  165. // this.polygonClick();
  166. // if (
  167. // this.$store.state.map2DViewerPolygonsCid.newCid &&
  168. // this.polygon[this.$store.state.map2DViewerPolygonsCid.newCid]
  169. // ) {
  170. // this.polygon[this.$store.state.map2DViewerPolygonsCid.newCid].setStyle({
  171. // color: this.$store.state.map2DViewerPolygonsCid.oldColor,
  172. // fillOpacity: 0
  173. // });
  174. // }
  175. // // 判断是否是重复点击,不是的话高亮,是的话不操作。
  176. // if (cid != this.$store.state.map2DViewerPolygonsCid.newCid) {
  177. // this.$store.state.map2DViewerPolygonsCid = { newCid: cid, oldColor: v.color };
  178. // polygon.setStyle({
  179. // color: "red",
  180. // fillColor: "red",
  181. // fillOpacity: 0.35
  182. // });
  183. // } else {
  184. // this.$store.state.map2DViewerPolygonsCid = { newCid: null, oldColor: null };
  185. // }
  186. if (v.geometry && JSON.parse(v.geometry).properties) {
  187. this.$store.state.mapMarkInfo = JSON.parse(v.geometry).properties;
  188. }
  189. });
  190. this.polygon[cid] = polygon;
  191. });
  192. }
  193. }
  194. }
  195. });
  196. },
  197. polygonClick() {},
  198. //geojson直接读取的坐标顺序是经纬度
  199. // 明天测试一下对不对
  200. readGeojson(geojsonArr, color) {
  201. let geojsonData = L.geoJSON(geojsonArr, {
  202. style: function (feature) {
  203. return { color: color };
  204. }
  205. }).addTo(this.map);
  206. this.polygon.push(geojsonData);
  207. },
  208. mapInit() {
  209. var crs = new L.Proj.CRS(
  210. "EPSG:0986",
  211. "+proj=tmerc +lat_0=0 +lon_0=121.2751921 +k=1 +x_0=0 +y_0=-3457147.81 +ellps=krass +units=m +no_defs",
  212. {
  213. resolutions: [
  214. 132.2919312505292, 52.91677250021167, 26.458386250105836, 13.229193125052918, 5.291677250021167, 2.6458386250105836,
  215. 1.3229193125052918, 0.5291677250021167, 0.26458386250105836, 0.13229193125052918, 0.0529167725002, 0.0264583862501,
  216. 0.0132291931251, 0.00529167725, 0.002645838625, 0.0013229193125
  217. ],
  218. origin: [-66000, 75000],
  219. bounds: L.bounds([-65000, -76000], [75000, 72000])
  220. }
  221. );
  222. this.map = L.map(this.$refs.map, {
  223. crs: crs,
  224. zoom: 1,
  225. minZoom: 0,
  226. maxZoom: 14,
  227. attributionControl: false,
  228. zoomControl: false
  229. }).setView(map2DViewer.map.getCenter(), map2DViewer.map.getZoom());
  230. //添加默认图层
  231. let layer = this.$store.state.mapMethodsCollection.get("RENDER").addTiledMapLayer(this.mapUrl);
  232. this.layers = layer;
  233. },
  234. setView: function (coord, zoom) {
  235. this.$nextTick(() => {
  236. let center = L.latLng(coord[0], coord[1]);
  237. this.map.setView(center, zoom);
  238. });
  239. },
  240. // 更改地图渲染个数
  241. changeMapSize() {
  242. this.$nextTick(() => {
  243. if (this.map) {
  244. this.map.removeLayer(this.layers);
  245. let layer = this.$store.state.mapMethodsCollection.get("RENDER").addTiledMapLayer(this.mapUrl).addTo(this.map);
  246. this.layers = layer;
  247. this.map.invalidateSize(true);
  248. }
  249. });
  250. }
  251. },
  252. watch: {
  253. centerZoom: {
  254. handler(newValue, oldValue) {
  255. if (newValue.center && newValue.zoom && newValue.index != this.index) {
  256. this.setView(newValue.center, newValue.zoom);
  257. }
  258. },
  259. deep: true
  260. },
  261. centerZoomInit: {
  262. handler(newValue, oldValue) {
  263. if (newValue.lat && newValue.lng && newValue.zoom) {
  264. let center = L.latLng(newValue.lat, newValue.lng);
  265. this.map.setView(center, newValue.zoom);
  266. }
  267. },
  268. deep: true
  269. }
  270. }
  271. };
  272. </script>
  273. <style scoped>
  274. .MapViewer {
  275. width: 100%;
  276. height: 100%;
  277. background-color: #003452;
  278. }
  279. </style>