MapView.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view id="container" class="" :prop="maplist" :change:prop="mapModule.addGeoJson" :item="mapitem"
  3. :change:item="mapModule.flyto">
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. props: {
  9. // maplist: {
  10. // type: Array,
  11. // default: []
  12. // },
  13. },
  14. data() {
  15. return {
  16. maps: {},
  17. maplist: [],
  18. mapitem: {},
  19. };
  20. },
  21. mounted() {
  22. // console.log(mapModule, 'zmapModule')
  23. },
  24. methods: {
  25. setlist(list) {
  26. console.log(list, 'list')
  27. this.maplist = list
  28. },
  29. setitem(val) {
  30. console.log(val, 'val')
  31. if(val)this.mapitem = val
  32. },
  33. }
  34. }
  35. </script>
  36. <script module="mapModule" lang="renderjs">
  37. // import Axios from "../../static";
  38. import "ol/ol.css";
  39. import {
  40. transform
  41. } from "ol/proj.js";
  42. import Map from "ol/Map";
  43. import View from "ol/View";
  44. // import TileLayer from "ol/layer/Tile";
  45. import XYZ from "ol/source/XYZ";
  46. import Tile from "ol/layer/Tile";
  47. import OSM from 'ol/source/OSM';
  48. import parse from "wellknown";
  49. import VectorLayer from "ol/layer/Vector";
  50. import VectorSource from "ol/source/Vector";
  51. import {
  52. Style,
  53. Icon
  54. } from "ol/style";
  55. import Stroke from "ol/style/Stroke";
  56. import GeoJSON from "ol/format/GeoJSON";
  57. let map = null;
  58. export default {
  59. name: "map-view",
  60. data() {
  61. return {
  62. maps: {},
  63. vectorLayer: {},
  64. curPageResultLayer: {}
  65. };
  66. },
  67. mounted() {
  68. this.createMap();
  69. },
  70. methods: {
  71. createMap() {
  72. var view = new View({
  73. center: transform([116.40, 39.91], "EPSG:4326", "EPSG:3857"), //地图初始中心点
  74. projection: "EPSG:3857",
  75. zoom: 15,
  76. minZoom: 2,
  77. maxZoom: 18
  78. });
  79. this.addMap("container", view);
  80. },
  81. addMap(target, view) {
  82. if (this.maps[target]) return;
  83. var key = "12df6e32906dbb916fad14dc65ebdbf8"; // 请替换成你的key
  84. // 矢量底图
  85. var vecLayer = new Tile({
  86. source: new XYZ({
  87. //url: 'http://{s}.tile.osm.org/{z}/{x}/{y}.png'
  88. //'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
  89. url: "http://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=" +
  90. key,
  91. }),
  92. });
  93. // 矢量标注
  94. var cvaLayer = new Tile({
  95. source: new XYZ({
  96. url: "http://t0.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=" +
  97. key,
  98. }),
  99. });
  100. var lyr = new Tile({
  101. source: new XYZ({
  102. //url: "http://49.232.215.206:3001/static/tiles/gg/{z}/{x}/{-y}.png"
  103. url: "../doc/tiles/{z}/{x}/{-y}.png"
  104. }),
  105. });
  106. let map = new Map({
  107. //地图容器div的ID
  108. target,
  109. //地图容器中加载的图层
  110. layers: [vecLayer, cvaLayer, lyr],
  111. //地图视图设置
  112. view, //同一个view
  113. });
  114. this.maps[target] = map;
  115. // uni.$globalData = this.maps
  116. // this.$ownerInstance.callMethod('setmap', this.maps);
  117. this.initVectorLayer()
  118. // window.map = this.maps;
  119. // console.log(window.map, "window.map", 'lyr');
  120. },
  121. getMap() {
  122. return this.maps
  123. },
  124. initVectorLayer(name = 'container') {
  125. if (!this.vectorLayer[name]) {
  126. let vectorSource = new VectorSource({
  127. // projection: "EPSG:3857",
  128. });
  129. this.vectorLayer[name] = new VectorLayer({
  130. source: vectorSource,
  131. style: new Style({
  132. stroke: new Stroke({
  133. color: "#f00",
  134. width: 2,
  135. }),
  136. }),
  137. });
  138. this.maps[name].addLayer(this.vectorLayer[name]);
  139. }
  140. if (!this.curPageResultLayer[name]) {
  141. let vectorSource = new VectorSource();
  142. this.curPageResultLayer[name] = new VectorLayer({
  143. source: vectorSource,
  144. zIndex: 999,
  145. style: new Style({
  146. stroke: new Stroke({
  147. color: "#1f1cd3",
  148. width: 2,
  149. }),
  150. }),
  151. });
  152. this.maps[name].addLayer(this.curPageResultLayer[name]);
  153. }
  154. },
  155. //创建监测图斑列表实体
  156. addGeoJson(maplist, ) {
  157. maplist.forEach((titem, i) => {
  158. if (titem.geom && titem.geom != "") {
  159. if (typeof titem.geom === "string") {
  160. titem.geom = parse(titem.geom);
  161. }
  162. let features = new GeoJSON().readFeatures(titem.geom, {
  163. dataProjection: "EPSG:4326",
  164. featureProjection: "EPSG:3857"
  165. });
  166. var tempName = name + i;
  167. this.curPageResultLayer[tempName] = new VectorLayer({
  168. source: new VectorSource({
  169. features: features,
  170. }),
  171. style: function(feature) {
  172. return new Style({
  173. stroke: new Stroke({
  174. //边界样式
  175. color: "rgba(0, 0, 255, 1)",
  176. width: 2,
  177. }),
  178. });
  179. },
  180. zIndex: 9999,
  181. });
  182. this.maps.container.addLayer(this.curPageResultLayer[tempName]);
  183. }
  184. });
  185. if (maplist.length) {
  186. this.flytoByLayer(this.curPageResultLayer[0])
  187. }
  188. },
  189. flyto(item) {
  190. // window.map["mapDiv"]
  191. if (!this.maps['container']) return
  192. this.maps['container'].getView().animate({
  193. center: transform([item.lzb, item.bzb], "EPSG:4326", "EPSG:3857"),
  194. zoom: 18,
  195. duration: 20, // 动画持续时间,单位毫秒
  196. });
  197. },
  198. flytoByLayer(layer) {
  199. let fullExtent = layer
  200. .getSource()
  201. .getExtent();
  202. console.log('maplist', fullExtent)
  203. let map = this.maps.container
  204. map.getView().fit(fullExtent, {
  205. duration: 3, //动画的持续时间,
  206. callback: null,
  207. size: map.getSize(),
  208. padding: [100, 100, 100, 100],
  209. constrainResolution: false, // 允许视图超出分辨率限制
  210. });
  211. },
  212. },
  213. };
  214. </script>
  215. <style scoped>
  216. #container {
  217. width: 100vw;
  218. height: 40vh;
  219. }
  220. </style>