0c5a19c28252a41c94454c99ce21245b7a830893.svn-base 7.5 KB

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