| | |
| | | import WfsLayerService from './WfsLayerService' |
| | | import WmsLayerService from './WmsLayerService' |
| | | import { clone } from '../../../utils/utils' |
| | | import { logicMapper } from '../../../conf/Constants' |
| | | import { logicMapper, SERVICE_TYPE } from '../../../conf/Constants' |
| | | import WmsLayerService from './WmsLayerService' |
| | | |
| | | /** |
| | | * init 只初始化一次 |
| | | * start 每次调用图层显示show()时,都会调用 |
| | | * destory 每次调用图层隐藏hide()时,都会调用 |
| | | * |
| | | */ |
| | | class LayerFactory { |
| | | constructor (options) { |
| | | this.L = options.L |
| | |
| | | this.layers = {} |
| | | this.layersLogic = {} |
| | | this.minZoomLayers = {} |
| | | this.wmsLayers = [] |
| | | this.clickSwitch = true // 图层点击弹窗开关 |
| | | } |
| | | |
| | | init (layerConfig) { |
| | | this.initConfig(layerConfig) |
| | | // wms服务只需要初始化一次 |
| | | this.wmsLayerService = new WmsLayerService(this.wmsLayers) |
| | | this.wmsLayerService.init() |
| | | } |
| | | |
| | | initConfig (layerConfig) { |
| | | // 1. 遍历layer config |
| | | if (layerConfig) { |
| | | for (var i = 0; i < layerConfig.length; i++) { |
| | |
| | | var layers = config.layers |
| | | var childLayer = config.childLayer |
| | | var checked = config.checked |
| | | /* if (config.groupName) { |
| | | const wmsGroupLayerService = new WmsGroupLayerService(config) |
| | | wmsGroupLayerService.init() |
| | | continue |
| | | } */ |
| | | layers && this.init(config.layers) |
| | | childLayer && this.init(config.childLayer) |
| | | |
| | |
| | | initMinZoom (config) { |
| | | const minZoom = parseInt(config.minZoom) |
| | | if (minZoom) { |
| | | this.minZoomLayers[minZoom] = config |
| | | var configs = this.minZoomLayers[minZoom] |
| | | if (configs) { |
| | | configs[configs.length] = config |
| | | } else { |
| | | configs = [config] |
| | | } |
| | | this.minZoomLayers[minZoom] = configs |
| | | } |
| | | } |
| | | |
| | | loadLogic (config) { |
| | | var code = config.code |
| | | var wfs = config.wfs |
| | | var wms = config.wms |
| | | var type = config.type |
| | | |
| | | const file = logicMapper[code] |
| | | var logic = this.layersLogic[code] |
| | |
| | | if (file) { |
| | | var BusiLayer = require('../logic/' + file) |
| | | logic = new BusiLayer() |
| | | } else if (wfs) { |
| | | } else if (type === SERVICE_TYPE.WFS) { |
| | | logic = new WfsLayerService(config) |
| | | } else if (wms) { |
| | | logic = new WmsLayerService(config) |
| | | } else if (type === SERVICE_TYPE.WMS) { |
| | | var layer = {} |
| | | layer[config.code] = config |
| | | this.wmsLayers.push(layer) |
| | | } |
| | | } |
| | | this.layersLogic[code] = logic |
| | | return logic |
| | | } |
| | | |
| | | /** |
| | | * 1. 先调用处理逻辑的 initLayer ,如果没有 就创建一个 featureGroup |
| | | * 2. 如果存在事件逻辑的话,绑定tooltip,click事件 |
| | | * 3. 将layer添加到map |
| | | * 4. 返回layer |
| | | * @param config |
| | | * @returns layer |
| | | */ |
| | | addLayer (config) { |
| | | var code = config.code |
| | | var logic = this.loadLogic(config) |
| | |
| | | return layer |
| | | } |
| | | |
| | | /** |
| | | * 如果 存在已经加载了的对象,就直接加到map |
| | | * 如果 不存在则 调用 addLayer 及 逻辑类的init 进行初始化操作 |
| | | * 如果 存在start函数,则调用 |
| | | * @param config |
| | | */ |
| | | show (config) { |
| | | var index = config.index |
| | | var layer = this.layers[config.code] |
| | | var logic = this.loadLogic(config) |
| | | if (layer) { |
| | | if (!this.map.hasLayer(layer)) { |
| | | index && layer.setZIndex(index) |
| | | layer.addTo(this.map) |
| | | } |
| | | } else { |
| | | logic && logic.init(this.addLayer(config), this.L) |
| | | } |
| | | logic && logic.create && logic.create() |
| | | logic && logic.start && logic.start() |
| | | this.wmsLayerService && this.wmsLayerService.add(config) |
| | | } |
| | | |
| | | hide (config) { |
| | |
| | | layer && this.map.removeLayer(layer) |
| | | const logic = this.loadLogic(config) |
| | | logic && logic.destory && logic.destory() |
| | | this.wmsLayerService && this.wmsLayerService.remove(config) |
| | | } |
| | | |
| | | /** |
| | |
| | | toggleByZoom () { |
| | | const zoom = this.map.getZoom() |
| | | for (var k in this.minZoomLayers) { |
| | | const config = this.minZoomLayers[k] |
| | | const checked = config.checked |
| | | console.log(zoom) |
| | | console.log(k) |
| | | if (checked && zoom > k) { |
| | | this.show(config) |
| | | } else if (checked && zoom < k) { |
| | | this.hide(config) |
| | | const configs = this.minZoomLayers[k] |
| | | for (var j in configs) { |
| | | const config = configs[j] |
| | | const checked = config.checked |
| | | // console.log(zoom) |
| | | // console.log(k) |
| | | if (checked && zoom > k) { |
| | | this.show(config) |
| | | } else if (checked && zoom < k) { |
| | | this.hide(config) |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | break |
| | | case 'MultiLineString': |
| | | var coordinates = feature.geometry.coordinates |
| | | point = coordinates[parseInt(coordinates.length / 2)][0] |
| | | var coordinate = coordinates[parseInt(coordinates.length / 2)][0] |
| | | if (coordinate.length > 2) { |
| | | point = [coordinate[0], coordinate[1]] |
| | | } |
| | | break |
| | | case 'LineString': |
| | | var lineString = feature.geometry.coordinates |
| | | point = lineString[parseInt(lineString.length / 2)][0] |
| | | break |
| | | } |
| | | window.map.flyTo(point.reverse(), 15) |
| | | console.log(point) |
| | | window.map.flyTo(point.reverse(), 17) |
| | | code && this.openPopup(code, feature.id) |
| | | } |
| | | |
| | |
| | | this.findLayerById(layer.getLayers(), id) |
| | | } else { |
| | | layer.eachLayer(function (layer) { |
| | | console.log(layer) |
| | | // console.log(layer) |
| | | }) |
| | | } |
| | | } |