移动端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

404 行
12 KiB

  1. <template>
  2. <div class="mapBox">
  3. <div :id="'map-element' + elementId" class="map-element" style="width: 100%; height: 100%;"></div>
  4. <div :id="'draw-toolbar' + elementId" class="mapBox_button" v-if="allowDraw">
  5. <van-button :id="'drawPolygon' + elementId" native-type="button" icon="../../../static/images/icon/icon_ht.png" type="default" block round size="small"></van-button>
  6. <van-button :id="'drawRemove' + elementId" native-type="button" icon="cross" type="default" block round size="small" style="margin-top: 0.2rem"></van-button>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import $ from "jquery";
  12. const COMMON_MAP_DRAW_LAYER_NAME = '_Draw_layer';
  13. export default {
  14. name: "CommonMap",
  15. props: {
  16. minMapZoom: {
  17. type: Number,
  18. default: 16,
  19. },
  20. maxMapZoom: {
  21. type: Number,
  22. default: 18.9,
  23. },
  24. allowDraw: {
  25. type: Boolean,
  26. default: false,
  27. },
  28. coord: {
  29. type: Array,
  30. default: function() {
  31. return [115.452752, 31.789033];
  32. },
  33. },
  34. },
  35. data() {
  36. return {
  37. isInited: false,
  38. mapObject: null,
  39. userLayers: [],
  40. drawResult: null,
  41. elementId: ('_' + Math.random()).replaceAll('.', ''),
  42. };
  43. },
  44. created() {
  45. },
  46. mounted() {
  47. this.init();
  48. },
  49. methods: {
  50. init() {
  51. if(this.isInited)
  52. return;
  53. this.createMap();
  54. this.setDraw(true);
  55. this.isInited = true;
  56. },
  57. destroyMap() {
  58. if(!this.mapObject)
  59. return;
  60. delete this.mapObject;
  61. document.getElementById("map-element" + this.elementId).innerHTML = '';
  62. this.mapObject = null;
  63. },
  64. createMap() {
  65. if(this.mapObject)
  66. return;
  67. let projection = new ol.proj.Projection({
  68. //地图投影类型
  69. code: "EPSG:3857",
  70. units: "degrees",
  71. //extent:extent
  72. });
  73. let aerial = new ol.layer.Tile({
  74. source: new ol.source.XYZ({
  75. url: "http://t0.tianditu.gov.cn/img_w/wmts?" +
  76. "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" +
  77. "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=cc4aba6e967096098249efa069733067",
  78. }),
  79. isGroup: true,
  80. name: "卫星影像图",
  81. });
  82. let yingxzi = new ol.layer.Tile({
  83. source: new ol.source.XYZ({
  84. url: "https://t0.tianditu.gov.cn/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=cc4aba6e967096098249efa069733067",
  85. }),
  86. isGroup: true,
  87. name: "天地图文字标注--卫星影像图",
  88. });
  89. //加载地图
  90. let map = new ol.Map({
  91. // interactions: ol.interaction.DragPan({
  92. // onFocusOnly: false
  93. // }),
  94. controls: ol.control.defaults({attribution: false, zoom: false, rotate: false}).extend([]), //隐藏放大缩小按钮
  95. layers: [aerial, yingxzi],
  96. projection: projection,
  97. target: "map-element" + this.elementId,
  98. logo: 'false',
  99. view: new ol.View({
  100. center: ol.proj.fromLonLat(this.coord),
  101. //zoom: false,
  102. zoom: 17,
  103. minZoom: this.minMapZoom, //地图缩小限制
  104. maxZoom: this.maxMapZoom, //地图放大限制
  105. }),
  106. });
  107. this.mapObject = map;
  108. //(map);
  109. //console.log(map.getProperties());
  110. },
  111. setCoord(lng, lat) {
  112. if(!this.mapObject)
  113. return false;
  114. this.mapObject.getView().setCenter(ol.proj.fromLonLat([lng, lat]));
  115. //console.log('setCoord', lng, lat,this.mapObject);
  116. },
  117. getProjection() {
  118. if(!this.mapObject)
  119. return null;
  120. return this.mapObject.get('view').getProjection();
  121. },
  122. pushLayer(name, layer) {
  123. if(!this.mapObject)
  124. return false;
  125. if(this.checkNameExists(name))
  126. {
  127. console.error(name + ' is dup!');
  128. return false;
  129. }
  130. this.userLayers.push({
  131. name: name,
  132. layer: layer,
  133. visible: true,
  134. });
  135. this.mapObject.addLayer(layer);
  136. //console.log(name, layer);
  137. return true;
  138. },
  139. checkNameExists(name) {
  140. if(this.userLayers.length === 0)
  141. return false;
  142. let res = this.userLayers.findIndex((x) => {
  143. return x.name === name;
  144. });
  145. return res !== -1;
  146. },
  147. findLayer(name = null) {
  148. if(this.userLayers.length === 0)
  149. return null;
  150. if(name) {
  151. for(let i in this.userLayers) {
  152. if(this.userLayers[i].name == name) {
  153. return this.userLayers[i];
  154. }
  155. }
  156. }
  157. else {
  158. let last = this.userLayers.length - 1;
  159. return this.userLayers[last];
  160. }
  161. return null;
  162. },
  163. takeLayer(name = null) {
  164. if(this.userLayers.length === 0)
  165. return null;
  166. if(name) {
  167. for(let i in this.userLayers) {
  168. if(this.userLayers[i].name == name) {
  169. let layer = this.userLayers[i];
  170. this.userLayers.splice(i, 1);
  171. return layer;
  172. }
  173. }
  174. }
  175. else {
  176. let last = this.userLayers.length - 1;
  177. let layer = this.userLayers[last];
  178. this.userLayers.splice(last, 1);
  179. return layer;
  180. }
  181. return null;
  182. },
  183. popLayer(name = null) {
  184. let layer = this.takeLayer(name);
  185. if(layer)
  186. this.mapObject.removeLayer(layer.layer);
  187. },
  188. setLayerVisible(name, on) {
  189. let layer = this.findLayer(name);
  190. //console.log(layer.layer);
  191. if(!layer)
  192. return false;
  193. layer.layer.setVisible(on);
  194. return true;
  195. },
  196. hideLayer(name = null) {
  197. return this.setLayerVisible(name, false);
  198. },
  199. showLayer(name = null) {
  200. return this.setLayerVisible(name, true);
  201. },
  202. setDraw(on) {
  203. if(!this.mapObject)
  204. return;
  205. /* if(this.allowDraw === on)
  206. return;*/
  207. if(on) {
  208. let self = this;
  209. let draw;
  210. // 不要使用 $('DOM-selector').click(function), 动态加载按钮时手机端会失效
  211. $(document).on('click', "#drawPolygon" + this.elementId, function () {
  212. self.drawResult = null;
  213. self.$emit("drawStarted");
  214. self.popLayer(COMMON_MAP_DRAW_LAYER_NAME);
  215. let drawLayer = new ol.layer.Vector({
  216. source: new ol.source.Vector(),
  217. });
  218. self.pushLayer(COMMON_MAP_DRAW_LAYER_NAME, drawLayer);
  219. draw = new ol.interaction.Draw({
  220. source: drawLayer.getSource(),
  221. type: "Polygon"
  222. });
  223. draw.on('drawend', function (evt) {
  224. if(self.drawInsert != null){
  225. $("#drawRemove" + self.elementId).trigger('click');
  226. }
  227. let feature = evt.feature;
  228. let geometry = feature.getGeometry();
  229. let coordinate = geometry.getCoordinates();
  230. self.drawResult = coordinate;
  231. self.$emit("drawFinished", coordinate);
  232. });
  233. self.mapObject.addInteraction(draw);
  234. //console.log("drawPolygon");
  235. });
  236. //清除画图鼠标点击事件
  237. $(document).on('click', "#drawRemove" + this.elementId, function () {
  238. self.drawInsert = null;
  239. self.mapObject.removeInteraction(draw);
  240. self.popLayer(COMMON_MAP_DRAW_LAYER_NAME);
  241. self.$emit("drawFinished", null);
  242. //console.log("drawRemove");
  243. });
  244. //还原之前图层
  245. /*$("#drawReset" + this.elementId).click(function () {
  246. self.popLayer(COMMON_MAP_DRAW_LAYER_NAME);
  247. self.drawResult = null;
  248. self.$emit("drawReseted");
  249. //console.log("drawReset");
  250. });*/
  251. }
  252. else
  253. {
  254. self.popLayer(COMMON_MAP_DRAW_LAYER_NAME);
  255. self.drawResult = null;
  256. }
  257. },
  258. setLayer(name, theGeom = null) {
  259. this.popLayer(name);
  260. if(theGeom)
  261. this.addLayer(name, theGeom);
  262. },
  263. addLayer(name, theGeom) {
  264. if(!this.mapObject)
  265. return false;
  266. //地图只加载一次
  267. //加载地图编辑
  268. //图层查询定位开始 ---------start
  269. let hc_land = new ol.layer.Vector({
  270. title: name,
  271. source: new ol.source.Vector({
  272. projection: this.getProjection(),
  273. features: new ol.format.GeoJSON().readFeatures("{\n" +
  274. " \"type\": \"Feature\",\n" +
  275. " \"geometry\":" + theGeom + "}"),
  276. }),
  277. style: new ol.style.Style({
  278. fill: new ol.style.Fill({
  279. //矢量图层填充颜色,以及透明度
  280. color: "rgba(204, 255, 204,0.3)",
  281. }),
  282. stroke: new ol.style.Stroke({
  283. //边界样式
  284. color: "#47c68f",
  285. width: 3,
  286. }),
  287. }),
  288. });
  289. if(!this.pushLayer(name, hc_land))
  290. return false;
  291. var maxXMap = hc_land.values_.source.featuresRtree_.rbush_.data.maxX;
  292. var maxYMap = hc_land.values_.source.featuresRtree_.rbush_.data.maxY;
  293. var minXMap = hc_land.values_.source.featuresRtree_.rbush_.data.minX;
  294. var minYMap = hc_land.values_.source.featuresRtree_.rbush_.data.minY;
  295. //定位查询位置
  296. var center = ol.extent.getCenter([maxXMap, maxYMap, minXMap, minYMap]); //获取边界区域的中心位置
  297. this.mapObject.getView().animate({
  298. // 只设置需要的属性即可
  299. center: center, // 中心点
  300. zoom: 17, // 缩放级别
  301. rotation: undefined, // 缩放完成view视图旋转弧度
  302. duration: 300, // 缩放持续时间,默认不需要设置
  303. });
  304. return true;
  305. },
  306. addLayerNew(name, theGeom,tcmc,status) {
  307. if(!this.mapObject)
  308. return false;
  309. let css = null;
  310. if(tcmc === "zjdzdxx"){
  311. if(status === "1"){
  312. css = "#F9F900";
  313. }else if(status === "2"){
  314. css = "#FF8000";
  315. }else if(status === "3"){
  316. css = "#82D900";
  317. }else if(status === "4"){
  318. css = "#F75000";
  319. }else{
  320. css = "#9F4D95";
  321. }
  322. }else if(tcmc === "fsss"){
  323. css = "#FF8C00";
  324. }else{
  325. css = "#47c68f";
  326. }
  327. //地图只加载一次
  328. //加载地图编辑
  329. //图层查询定位开始 ---------start
  330. console.info(css);
  331. let hc_land = new ol.layer.Vector({
  332. title: name,
  333. source: new ol.source.Vector({
  334. projection: this.getProjection(),
  335. features: new ol.format.GeoJSON().readFeatures("{\n" +
  336. " \"type\": \"Feature\",\n" +
  337. " \"geometry\":" + theGeom + "}"),
  338. }),
  339. style: new ol.style.Style({
  340. fill: new ol.style.Fill({
  341. //矢量图层填充颜色,以及透明度
  342. color: "rgba(204, 255, 204,0.3)",
  343. }),
  344. stroke: new ol.style.Stroke({
  345. //边界样式
  346. color: css,
  347. width: 3,
  348. }),
  349. }),
  350. });
  351. if(!this.pushLayer(name, hc_land))
  352. return false;
  353. var maxXMap = hc_land.values_.source.featuresRtree_.rbush_.data.maxX;
  354. var maxYMap = hc_land.values_.source.featuresRtree_.rbush_.data.maxY;
  355. var minXMap = hc_land.values_.source.featuresRtree_.rbush_.data.minX;
  356. var minYMap = hc_land.values_.source.featuresRtree_.rbush_.data.minY;
  357. //定位查询位置
  358. var center = ol.extent.getCenter([maxXMap, maxYMap, minXMap, minYMap]); //获取边界区域的中心位置
  359. this.mapObject.getView().animate({
  360. // 只设置需要的属性即可
  361. center: center, // 中心点
  362. zoom: 17, // 缩放级别
  363. rotation: undefined, // 缩放完成view视图旋转弧度
  364. duration: 1000, // 缩放持续时间,默认不需要设置
  365. });
  366. return true;
  367. },
  368. update() {
  369. if(!this.mapObject)
  370. return;
  371. this.mapObject.updateSize();
  372. },
  373. }
  374. }
  375. </script>
  376. <style scoped lang="scss">
  377. .map-element {
  378. width: 100%;
  379. height: 100%;
  380. }
  381. .mapBox{
  382. position: relative;
  383. .mapBox_button{
  384. position: absolute;
  385. top: 0.2rem;
  386. right: 2%;
  387. }
  388. }
  389. </style>