b232186fb4e43bcf3c149403255f657da021e3a4.svn-base 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <simple-marker ref="simpleMarker"></simple-marker>
  3. </template>
  4. <script>
  5. import {LineString, MultiLineString, MultiPolygon, Polygon} from "ol/geom";
  6. import {Vector as VectorSource} from "ol/source";
  7. import {Vector as VectorLayer} from "ol/layer";
  8. import Style from "ol/style/Style";
  9. import Fill from "ol/style/Fill";
  10. import Stroke from "ol/style/Stroke";
  11. import Text from 'ol/style/Text';
  12. import CircleStyle from "ol/style/Circle";
  13. import Feature from "ol/Feature";
  14. import SimpleMarker from "@/components/BasicMap/Tools/SimpleMarker.vue";
  15. import * as bussLayerFn from '@/utils/oneMap/bussLayer';
  16. import {transform} from "ol/proj";
  17. import proj4 from "proj4";
  18. import {register} from "ol/proj/proj4";
  19. export default {
  20. name: "LocateByGeoinfo",
  21. components: {SimpleMarker},
  22. data() {
  23. return {
  24. map: {},
  25. vectorLayer: '',
  26. }
  27. },
  28. inject: ['baseMap'],
  29. beforeCreate() {
  30. proj4.defs("EPSG:4548",
  31. "+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs");
  32. proj4.defs("EPSG:4527",
  33. "+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=39500000 +y_0=0 +ellps=GRS80 +units=m +no_defs");
  34. register(proj4);
  35. },
  36. methods: {
  37. locate(record, map) {
  38. this.map = map;
  39. if (record.lng && record.lng && !record.geoinfo) {
  40. this.$refs.simpleMarker.hideMarker();
  41. let coord
  42. if (record.lng > 39000000) {
  43. coord = transform([record.lng,record.lat], 'EPSG:4527', 'EPSG:4490');
  44. } else if (record.lng > 180) {
  45. coord = transform([record.lng,record.lat], 'EPSG:4548', 'EPSG:4490');
  46. } else {
  47. coord=[record.lng,record.lat];
  48. }
  49. this.locateByCoords(coord);
  50. return;
  51. }
  52. let geoStr = record.geoinfo;
  53. if (!geoStr) {
  54. this.$message.warn('没有空间信息!')
  55. return;
  56. }
  57. if (record.layerName) {
  58. if (record.layerName.includes(',')) {
  59. let layerNames = record.layerName.split(',');
  60. layerNames.forEach((item) => {
  61. this.getTreeExpandKeys(this.baseMap.list, item);
  62. })
  63. } else {
  64. // 显示图层
  65. this.getTreeExpandKeys(this.baseMap.list, record.layerName);
  66. }
  67. }
  68. this.$refs.simpleMarker.hideMarker();
  69. this.clear();
  70. let jsonStr;
  71. let geomType;
  72. if (JSON.parse(geoStr).coordinates) {
  73. jsonStr = JSON.parse(geoStr).coordinates;
  74. geomType = JSON.parse(geoStr).type;
  75. }else{
  76. jsonStr = JSON.parse(geoStr).geometry.coordinates;
  77. geomType = JSON.parse(geoStr).geometry.type;
  78. }
  79. if(geomType==='Point'){
  80. if (jsonStr[0] > 39000000) {
  81. jsonStr = transform(jsonStr, 'EPSG:4527', 'EPSG:4490');
  82. } else if (jsonStr[0] > 180) {
  83. jsonStr = transform(jsonStr, 'EPSG:4548', 'EPSG:4490');
  84. } else {
  85. }
  86. }else {
  87. jsonStr.forEach((item,index)=>{
  88. if (item[0] > 39000000) {
  89. jsonStr[index] = transform(item, 'EPSG:4527', 'EPSG:4490');
  90. } else if (item[0] > 180) {
  91. jsonStr[index] = transform(item, 'EPSG:4548', 'EPSG:4490');
  92. } else {
  93. }
  94. })
  95. }
  96. let geom = null;
  97. switch (geomType) {
  98. case "Point":
  99. this.locateByCoords(jsonStr);
  100. break;
  101. case "MultiPolygon":
  102. geom = new MultiPolygon(jsonStr);
  103. this.locateByGeometry(geom);
  104. break;
  105. case "MultiLineString":
  106. geom = new MultiLineString(jsonStr);
  107. this.locateByGeometry(geom);
  108. break;
  109. case "Polygon":
  110. geom = new Polygon(jsonStr);
  111. this.locateByGeometry(geom);
  112. break;
  113. case "LineString":
  114. geom = new LineString(jsonStr);
  115. setTimeout(() => {
  116. this.$refs.simpleMarker.init2(jsonStr);
  117. }, 2000)
  118. this.locateByGeometry(geom);
  119. break;
  120. default:
  121. break;
  122. }
  123. },
  124. // 递归查找
  125. getTreeExpandKeys(obj, name) {
  126. if (obj && obj.length !== 0) {
  127. obj.forEach((item, index) => {
  128. if (item.alias === name) {
  129. item.visible = true;
  130. let m_layer = this.getLayerByName(name);
  131. if (!m_layer) {
  132. bussLayerFn.loadBussLayer(this.map, item.url, item.name, item.alias, item.sourceName,
  133. item.zIndex,
  134. item.visible);
  135. } else {
  136. m_layer.setVisible(true);
  137. }
  138. }
  139. // 如果此列表有children, 进行递归
  140. if (item.layers) {
  141. // console.log(index)
  142. this.getTreeExpandKeys(item.layers, name)
  143. }
  144. })
  145. }
  146. },
  147. getLayerByName(layerName) {
  148. var map = this.baseMap.map;
  149. if (!map) {
  150. return;
  151. }
  152. let layer = null;
  153. let layers = map.getLayers();
  154. let layerCount = layers.getLength();
  155. for (let i = 0; i < layerCount; i++) {
  156. let name = layers.item(i).get('alias');
  157. if (name === layerName) {
  158. layer = layers.item(i);
  159. return layer;
  160. }
  161. }
  162. return layer;
  163. },
  164. locateByCoords(coords) {
  165. this.map = this.baseMap.map;
  166. let view = this.map.getView();
  167. const zoom = view.getZoom();
  168. view.animate({
  169. zoom: zoom - 1,
  170. duration: 300
  171. }, {
  172. center: coords,
  173. zoom: 16,
  174. duration: 1700
  175. })
  176. setTimeout(() => {
  177. this.$refs.simpleMarker.init(coords);
  178. }, 2000)
  179. },
  180. locateByGeometry(geom) {
  181. if (this.vectorLayer === '') {
  182. const vectorSource = new VectorSource();
  183. this.vectorLayer = new VectorLayer({
  184. name: 'locMap',
  185. source: vectorSource,
  186. style: new Style({
  187. fill: new Fill({
  188. color: 'rgba(255, 255, 255, 0.6)',
  189. }),
  190. stroke: new Stroke({
  191. color: '#ff0000',
  192. lineDash: [6, 6],
  193. width: 5
  194. }),
  195. image: new CircleStyle({
  196. radius: 7,
  197. fill: new Fill({
  198. color: '#ffcc33',
  199. }),
  200. }),
  201. // text: new Text({
  202. // textAlign:'center',
  203. // textBaseline:'bottom',
  204. // font:'bold 2em Microsoft YaHei',
  205. // text:'文字提示',
  206. // placement:'line',
  207. // fill:new Fill({
  208. // color:'#FF0000'
  209. // })
  210. // })
  211. }),
  212. zIndex: 100
  213. })
  214. this.map.addLayer(this.vectorLayer);
  215. }
  216. let view = this.map.getView()
  217. let zoom = view.getZoom();
  218. if (geom == null) {
  219. return;
  220. }
  221. view.animate({
  222. zoom: zoom - 1,
  223. duration: 300
  224. });
  225. view.fit(geom, {
  226. size: [800, 600],
  227. duration: 1700
  228. // padding:[30,30,30,30]
  229. });
  230. let feature = null;
  231. let vectorSource = null;
  232. feature = new Feature({
  233. geometry: geom
  234. })
  235. vectorSource = new VectorSource();
  236. vectorSource.addFeature(feature);
  237. this.vectorLayer.setSource(vectorSource);
  238. this.vectorLayer.setZIndex(100);
  239. },
  240. clear() {
  241. if (this.vectorLayer) {
  242. this.vectorLayer.setSource(null);
  243. this.map.removeLayer(this.vectorLayer);
  244. this.vectorLayer = '';
  245. }
  246. }
  247. }
  248. }
  249. </script>
  250. <style scoped>
  251. .aa{
  252. font-family: "";
  253. }
  254. </style>