| | |
| | | import { markRaw, ref } from 'vue'; |
| | | import { MarkerOverlay } from './overlay/marker'; |
| | | import { Text } from 'ol/style'; |
| | | import { Select } from 'ol/interaction'; |
| | | import { click } from 'ol/events/condition'; |
| | | import { Polygon } from 'ol/geom'; |
| | | |
| | | export type LangType = 'zh_cn' | 'en'; |
| | | export const enum GaoDeSourceType { |
| | |
| | | }; |
| | | type MapConfig = { |
| | | sourceType: GaoDeSourceType; |
| | | markerIsVisible: boolean; |
| | | }; |
| | | |
| | | type OLEventType = 'blackClick' | 'featureChange' | 'featureHoverChange'; |
| | |
| | | map: OpenLayerMap; |
| | | source: XYZ; |
| | | private eventMap: Map<OLEventType, Function[]>; |
| | | /** @description 排除未做的layer,后续全部做好,可以不做判断 */ |
| | | unsupportedLayers = []; |
| | | |
| | | /** @description 图层控制信息 */ |
| | | layerInfo = ref([] as any[]); |
| | |
| | | themeInfo = ref([] as any[]); |
| | | |
| | | activeSourceType: Ref<GaoDeSourceType> = ref(GaoDeSourceType.Vector); |
| | | markerIsVisible: Ref<boolean> = ref(true); |
| | | interactLayer: VectorLayer<VectorSource<Feature<Geometry>>, Feature<Geometry>> = null; |
| | | |
| | | /** @description 当前激活状态 feature */ |
| | |
| | | } |
| | | |
| | | constructor(options: OLMapOptions) { |
| | | const { container, view, sourceType, markerIsVisible } = defaultsDeep(options, { |
| | | const { container, view, sourceType } = defaultsDeep(options, { |
| | | view: { |
| | | center: [13247019.404399557, 4721671.572580107], |
| | | zoom: 8, |
| | | }, |
| | | sourceType: GaoDeSourceType.Vector, |
| | | markerIsVisible: true, |
| | | } as OLMapOptions) as OLMapOptions; |
| | | this.source = new XYZ({ |
| | | crossOrigin: 'anonymous', |
| | |
| | | interactions: olDefaults({ doubleClickZoom: false }), |
| | | }); |
| | | this.activeSourceType.value = sourceType; |
| | | this.markerIsVisible.value = markerIsVisible; |
| | | this.applySourceType(this.activeSourceType.value); |
| | | this.listenMapClick(); |
| | | this.addBasicControl(); |
| | | } |
| | | |
| | | hightLightColor: '0000FFFF'; |
| | | sizeRate: 0; |
| | | |
| | | getPointHighLightStyle(feature) { |
| | | if (!feature) return null; |
| | | let pSize = feature.get('psize') ?? 15; |
| | | pSize += pSize * this.sizeRate; |
| | | |
| | | const pcolor = this.hightLightColor; |
| | | const pstyle = feature.get('pstyle'); |
| | | |
| | | return this.getPointStyles({ color: pcolor, size: pSize, style: pstyle }); |
| | | } |
| | | |
| | | getLineHighLightStyle(feature) { |
| | | if (!feature) return null; |
| | | |
| | | const lcolor = this.hightLightColor; |
| | | let lsize = feature.get('lsize') ?? 15; |
| | | lsize += lsize * this.sizeRate; |
| | | const lstyle = feature.get('lstyle'); |
| | | return this.getLineStyles({ lsize: lsize, lcolor: lcolor, lstyle: lstyle }); |
| | | } |
| | | |
| | | getPolygonHighLightStyle(feature) { |
| | | if (!feature) return null; |
| | | |
| | | const lcolor = this.hightLightColor; |
| | | // return this.getPolygonStyles({ pcolor: lcolor, lstyle: 'default', lsize: 2 }); |
| | | return new Style({ |
| | | stroke: new Stroke({ |
| | | color: lcolor, |
| | | width: 5, |
| | | }), |
| | | // fill: new Fill({ |
| | | // color: lcolor, |
| | | // }), |
| | | }); |
| | | } |
| | | |
| | | // 保存点样式缓存 |
| | |
| | | */ |
| | | getPointStyles(config: { size; color; style }) { |
| | | const pSize = config.size; |
| | | if (!pSize) return null; |
| | | |
| | | const pcolor = config.color; |
| | | const pstyle = config.style; |
| | |
| | | this.pointStyeMap[pkey] = new Style({ |
| | | text: new Text({ |
| | | font: `${pSize}px "ywifont"`, |
| | | |
| | | text: config?.text ?? '', |
| | | fill: new Fill({ color: `#${pcolor}` }), |
| | | }), |
| | |
| | | * 根据线的配置获取样式 |
| | | * @param config |
| | | */ |
| | | getLineStyles(config: { lsize; lcolor; lstyle }) { |
| | | getLineStyles(config: { lsize; lcolor; lstyle; psize?; pcolor?; pstyle? }) { |
| | | const lSize = config.lsize; |
| | | if (!lSize) return null; |
| | | const lColor = config.lcolor; |
| | | const lstyle = config.lstyle; |
| | | |
| | | const styles = []; |
| | | const lKey = `${lstyle}_${lSize}_${lColor}`; |
| | | if (!this.lineStyleMap[lKey]) { |
| | | const drawStyle = this.drawStyles.value.find((item) => item.id === lstyle + ''); |
| | |
| | | }), |
| | | }); |
| | | } |
| | | return [this.lineStyleMap[lKey]]; |
| | | styles.push(this.lineStyleMap[lKey]); |
| | | |
| | | const hasPoint = Object.keys(config).some((key) => key === 'psize' && !!config[key]); |
| | | if (hasPoint) { |
| | | const psize = config.psize; |
| | | const pcolor = config.pcolor; |
| | | const pstyle = config.pstyle; |
| | | const pointStyle = this.getPointStyles({ size: psize, color: pcolor, style: pstyle }); |
| | | styles.push(pointStyle); |
| | | } |
| | | return styles; |
| | | } |
| | | polygonStyleMap: Record<string, Style> = {}; |
| | | getPolygonStyles(config: { pcolor; lstyle; lsize; lcolor }) { |
| | | const styles = []; |
| | | const pColor = config.pcolor; |
| | | const key = pColor; |
| | | if (!this.polygonStyleMap[key]) { |
| | | const polygonStyle = new Style({ |
| | | fill: new Fill({ color: `#${pColor}` }), |
| | | }); |
| | | styles.push(polygonStyle); |
| | | } |
| | | |
| | | const lineStyle = this.getLineStyles({ |
| | | lsize: config.lsize, |
| | | lcolor: config.lcolor, |
| | | lstyle: config.lstyle, |
| | | }); |
| | | lineStyle && styles.push(...lineStyle); |
| | | return styles; |
| | | } |
| | | |
| | | private maxTextResolution = 1; |
| | |
| | | */ |
| | | getLabelStyles(config: { textContent; resolution }) { |
| | | const textContent = config.textContent; |
| | | if(!textContent) return null; |
| | | if (!textContent) return null; |
| | | const resolution = config.resolution; |
| | | if (!textContent) return null; |
| | | if (!this.labelStyleMap[textContent]) { |
| | | this.labelStyleMap[textContent] = this.createTextStyle(textContent, resolution); |
| | | } |
| | | return this.labelStyleMap[textContent] |
| | | return this.labelStyleMap[textContent]; |
| | | } |
| | | |
| | | setDrawStyles(styles: any[]) { |
| | |
| | | this.adjustViewToOverlays(markers); |
| | | } |
| | | |
| | | checkEquipIsShow() { |
| | | for (const item of this.layerInfo.value) { |
| | | if (item.id === 'equip') { |
| | | return item.isVisible; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | getEquipOverlay() { |
| | | for (const item of this.layerInfo.value) { |
| | | if (item.type === 'equip') { |
| | | return item; |
| | | } |
| | | } |
| | | } |
| | | createEleOverlay(dom: string | HTMLElement, position = [0, 0]) { |
| | | const ele = typeof dom === 'string' ? (document.querySelector(dom) as HTMLElement) : dom; |
| | | if (!ele) return; |
| | |
| | | stopEvent: false, |
| | | className: 'z-[999]', |
| | | }); |
| | | eleOverlay.setVisible(this.markerIsVisible.value); |
| | | eleOverlay.setVisible(this.checkEquipIsShow()); |
| | | |
| | | return eleOverlay; |
| | | } |
| | |
| | | const properties = feature.getProperties(); |
| | | console.log('🚀 ~ properties:', properties); |
| | | } |
| | | this.displayFeatureInfo(feature); |
| | | this.activeFeature.value = feature; |
| | | // this.displayFeatureInfo(feature); |
| | | }); |
| | | } |
| | | |
| | |
| | | this.themeInfo.value = themeInfo; |
| | | } |
| | | |
| | | getAllLayers() { |
| | | const allLayers = this.layerInfo.value.reduce((preVal, curVal) => { |
| | | if (curVal.children && curVal.children.length > 0) { |
| | | return preVal.concat(curVal.children.map((item) => item)); |
| | | } else { |
| | | return preVal; |
| | | } |
| | | }, []); |
| | | return allLayers; |
| | | } |
| | | |
| | | getAllLayerModels() { |
| | | console.log('🚀 ~ this.layerInfo.value:', this.layerInfo.value); |
| | | return this.layerInfo.value.reduce((preVal, curVal) => { |
| | | if (curVal.children && curVal.children.length > 0) { |
| | | return preVal.concat(curVal.children.map((item) => item.model)); |
| | | } else { |
| | | return preVal; |
| | | } |
| | | }, []); |
| | | } |
| | | |
| | | /** @description 记录所有图层 */ |
| | | setAllLayers(layerModels: Layer[], layers: any[], layerGroup: any[]) { |
| | | // this.layerInfo.value = layerModels.map((layer, index) => { |
| | | // const layerData = layers[index]; |
| | | |
| | | // return { |
| | | // id: layerData.id, |
| | | // title: layerData.title, |
| | | // model: markRaw(layer), |
| | | // get isVisible() { |
| | | // return layer.getVisible(); |
| | | // }, |
| | | // set isVisible(val) { |
| | | // layer.setVisible(val); |
| | | // }, |
| | | // }; |
| | | // }); |
| | | this.layerInfo.value = layerGroup.reduce((preVal, curVal) => { |
| | | const group = curVal.group; |
| | | const groupId = `group-${group}`; |
| | |
| | | mapGroupItem.children.push(data); |
| | | return preVal; |
| | | }, []); |
| | | const that = this; |
| | | this.layerInfo.value.push({ |
| | | id: 'equip', |
| | | title: '监测设备', |
| | | children: [], |
| | | _isVisible: true, |
| | | get isVisible() { |
| | | return this._isVisible; |
| | | }, |
| | | set isVisible(val) { |
| | | this._isVisible = val; |
| | | that.toggleMarkerOverlayVisible(val); |
| | | }, |
| | | type: 'equip', |
| | | }); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | }); |
| | | this.markerIsVisible.value = visible; |
| | | } |
| | | |
| | | getConfig(): MapConfig { |
| | | return { |
| | | sourceType: this.activeSourceType.value, |
| | | markerIsVisible: this.markerIsVisible.value, |
| | | }; |
| | | } |
| | | |
| | | setConfig(config: MapConfig) { |
| | | this.activeSourceType.value = config.sourceType; |
| | | this.markerIsVisible.value = config.markerIsVisible; |
| | | this.applySourceType(this.activeSourceType.value); |
| | | this.toggleMarkerOverlayVisible(this.markerIsVisible.value); |
| | | } |
| | | |
| | | private addBasicControl() { |
| | |
| | | displayFeatureInfo(feature) { |
| | | const highlightStyle = { |
| | | Point: new Style({ |
| | | // image: new Circle({ |
| | | // radius: 5, |
| | | // fill: new Fill({ color: `blue` }), |
| | | // }), |
| | | image: new Circle({ |
| | | radius: 5, |
| | | fill: new Fill({ color: `blue` }), |
| | | radius: 13, |
| | | stroke: new Stroke({ |
| | | color: `blue`, |
| | | width: 3, |
| | | }), |
| | | }), |
| | | }), |
| | | LineString: new Style({ |
| | |
| | | color: `blue`, |
| | | width: 5, |
| | | }), |
| | | }), |
| | | Polygon: new Style({ |
| | | stroke: new Stroke({ |
| | | color: `blue`, |
| | | width: 5, |
| | | }), |
| | | // fill: new Fill({ |
| | | // color: `rgba(0, 0, 255, 0.1)`, |
| | | // }), |
| | | }), |
| | | }; |
| | | // 创建高亮图层 |
| | |
| | | // 设置高亮要素 |
| | | if (feature !== this.activeFeature.value) { |
| | | if (this.activeFeature.value) { |
| | | console.log('remove feature:', this.activeFeature.value); |
| | | this.interactLayer.getSource().removeFeature(this.activeFeature.value as any); |
| | | } |
| | | if (feature) { |