import AjaxUtils from '../../../utils/AjaxUtils' import { PIPELINE_WMS } from '../../../conf/Constants' /** * wms 图层组的管理 */ class WmsLayerGroupService { constructor (config) { this.config = config this.L = window.L // 存放getfeatureinfo的图层组 this.featureGroup = this.L.featureGroup({}) this.map = window.map this.popupComp = window.popupComp // {groupName:{url:'.../wms',layers:[]}} this.map.on('click', (e) => this.click(e)) } load () { for (var k in this.groups) { console.log(k) this.L.tileLayer.wms(PIPELINE_WMS, { format: 'image/png', // 返回的数据格式 transparent: true, maxZoom: 21, layers: k // todo }).addTo(this.layer).bringToFront() } } init () { this.layer = this.L.featureGroup({}).addTo(this.map) this.initGroup(this.config) this.load() } hide (config) { const layerName = config.layerName for (var i = 0; i < this.groups.length; i++) { const group = this.groups[i] for (var k in group) { const v = group[k] const index = v.layers.indexOf(layerName) if (index >= 0) { delete v.layers[index] } } } } initGroup (config) { const groupName = this.config.groupName const layers = config.layers layers && this.initGroup(layers) for (var i = 0; i < config.length; i++) { const layerConfig = config[i] const layerName = layerConfig.layerName const group = this.groups[groupName] if (group) { group.layers.push(layerName) } else { this.groups[groupName] = { url: '', layers: [layerName] } } } } click (e) { this.featureGroup.clearLayers() var point = this.map.latLngToContainerPoint(e.latlng, this.map.getZoom()) var size = this.map.getSize() // const bbox = this.L.latLngBounds(this.L.latLng(e.latlng.lng, e.latlng.lat)).toBBoxString() const params = { VERSION: '1.1.1', SERVICE: 'WMS', REQUEST: 'GetFeatureInfo', // bbox: bbox, FORMAT: 'image/png', INFO_FORMAT: 'application/json', TRANSPARENT: true, LAYERS: 'sewer:pipeline_group', QUERY_LAYERS: 'sewer:pipeline_group', // todo FEATURE_COUNT: 50, SRS: 'EPSG:4326', WIDTH: size.x, HEIGHT: size.y, EXCEPTIONS: 'application/vnd.ogc.se_inimage', X: Math.round(point.x), Y: Math.round(point.y), BBOX: this.map.getBounds().toBBoxString() } AjaxUtils.get4JsonDataByUrl(PIPELINE_WMS, params, (res) => { let features = res.data.features features = features[0] var myIcon = this.L.divIcon({ className: 'my-div-icon' }) this.L.marker(features.geometry.coordinates.reverse(), { icon: myIcon }).addTo(this.featureGroup) .bindPopup((layer) => { this.popupComp.setDatas({ feature: features }) this.popupComp.setShow() return this.popupComp.$el }, { className: 's-map-popup', minWidth: 300, closeButton: false, autoClose: false }) .openPopup() }) } } export default WmsLayerGroupService