| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import Layer from 'ol/layer/Vector'
- import Source from 'ol/source/Vector'
- import Common from './Common'
- import Select from 'ol/interaction/Select.js';
- import {singleClick} from 'ol/events/condition'
- /**
- * @description KMap.VectorLayer 矢量图层
- */
- class VectorLayer {
- /**
- * @param {string} name 图层名称
- * @param {string} zIndex 图层层级
- */
- constructor(name,zIndex,options){
- let source = new Source({
- crossOrigin:'anonymous',
- })
- let ShowLevel = Common.ShowLevel
- let minZoom = ShowLevel[0]
- let maxZoom = ShowLevel[1]
- let style = null;
- if(options && options.source){
- source = options.source
- }
- if(options && options.minZoom){
- minZoom = options.minZoom
- }
- if(options && options.maxZoom){
- maxZoom = options.maxZoom
- }
- if(options && options.style){
- style = options.style
- }
- let layer = new Layer({
- source:source,
- zIndex:zIndex,
- minZoom:minZoom,
- maxZoom:maxZoom,
- })
- layer.set('name', name)
- if(style){
- layer.setStyle(style)
- }
- this.layer = layer
- this.source = source
- }
- setMaxZoom(maxZoom){
- this.layer.setMaxZoom(maxZoom);
- }
- setMinZoom(minZoom){
- this.layer.setMinZoom(minZoom);
- }
- addFeature(feature){
- this.source.addFeature(feature)
- }
- refresh(){
- this.source.refresh()
- }
- getFeatureById(id){
- return this.source.getFeatureById(id)
- }
- addSingleSelect(callback,map,style){
- let option = {
- condition:singleClick,
- layers:[this.layer],
- multi:false
- };
- if(style){
- option["style"] = style
- }
- this.singleSelect = new Select(option)
- this.singleSelect.on("select",callback)
- map.addInteraction(this.singleSelect)
- }
- addToggleSelect(callback,map,style){
- let option = {
- condition:singleClick,
- toggleCondition: singleClick,
- layers:[this.layer],
- multi:true
- };
- if(style){
- option["style"] = style
- }
- this.toggleSelect = new Select(option)
- this.toggleSelect.on("select",callback)
- map.addInteraction(this.toggleSelect)
- }
- }
- export default VectorLayer
|