mapLayerManager.js 6.27 KB
import layers from '@/assets/json/layers.json'
import graphicSymbol from '@/assets/json/graphicSymbol.json'
import queryUtils from "@libs/map/queryUtils";
import {loadModules} from "esri-loader"
import {maps} from '@/libs/map/mapUtils'

export default {

    methods:{
        getLayerByName(name) {
            for (var i = 0; i < layers.length; i++) {
                if (layers[i].layerName == name) {
                    return layers[i];
                }
            }
            return null;
        },
        addGeoByBsm(bsm, type,viewId) {
            var self = this;
            var layer = null;
            if (type == 'zd') {
                layer = this.getLayerByName("ZDJBXX");
            } else if (type == 'zrz') {
                layer = this.getLayerByName("ZRZ");
            } else {
                console.log("未定义类型!!");
                return;
            }
            if (!layer) {
                console.log("没有找到图层,不能查询");
                return;
            }
            var view = maps[viewId];
            queryUtils.methods.queryByWhere(layer.layerUrl+"/"+layer.id, {"BSM": bsm }, null, true, null, null, function (res) {
                var features = res.features;
                if(features && features.length > 0){
                    if( !features[0].geometry || features[0].geometry.rings.length == 0){
                        self.$message.success("暂无图形信息!!");
                        return;
                    }
                    loadModules([
                        "esri/layers/GraphicsLayer"
                    ]).then( ([
                                                      GraphicsLayer
                                                  ]) => {
                        var layer = view.map.findLayerById("highlightLayer");
                        if(layer){
                            layer.removeAll();
                        }else {
                            layer = new GraphicsLayer({
                                id:"highlightLayer"
                            })
                            view.map.add(layer,5);
                        }
                        var symbol = graphicSymbol.fillSymbol.highlightSymbol;
                        var graphic = features[0];
                        graphic.symbol = symbol;
                        layer.add(graphic);
                       // view.extent = graphic.geometry.extent;
                        view.center = graphic.geometry.extent.center;
                        view.zoom = 15;
                    }).catch( err => {
                        thow(err);
                    })
                }else {
                    self.$message.success("暂无图形信息!!");
                    return;
                }
            });
        },
        //将查询结果展示在地图上
        addSearchResultToMap(searchResult,viewId){
            var view = maps[viewId],
                self = this;
            loadModules([
                "esri/layers/GraphicsLayer"
            ]).then(([
                                     GraphicsLayer
                                 ]) => {
                if(!searchResult || searchResult.length == 0){
                    return;
                }
                for(var i = 0;i < searchResult.length;i++){
                    var layer = view.map.findLayerById("searchResult"+searchResult[i].layerId);
                    if(layer){
                        layer.removeAll();
                    }else{
                        layer = new GraphicsLayer({
                            id:"searchResult"+searchResult[i].layerId
                        })
                        view.map.add(layer);
                    }
                    var symbol = graphicSymbol.fillSymbol.highlightSymbol;
                    var features = searchResult[i].features;
                    for(var j = 0;j < features.length;j++){
                        if(features[j].geometry){
                            features[j].symbol = symbol;
                            layer.add(features[j]);
                        }

                    }
                }
            }).catch(err=>{
                console.log(err);
            })
        },
        //缩放至当前对象
        extentToGraphic(feature,highlightGraohic,viewId,callBackFunction){
            var symbol = graphicSymbol.fillSymbol.highlightSymbol;
            var highlightSymbol = graphicSymbol.fillSymbol.defaultSymbol;
            if(highlightGraohic){
                highlightGraohic.symbol = symbol;
            }
            feature.symbol = highlightSymbol;
            var view = maps[viewId];
            feature.geometry.type == 'point' ? view.center =  feature.geometry :view.center = feature.geometry.extent.center;
            if(callBackFunction && typeof callBackFunction == 'function'){
                callBackFunction();
            }
        },
        clearSearchLayer(viewId){
            var view = maps[viewId],
                layers = view.map.allLayers;
            layers.filter(function (item) {
                if(item.id.indexOf("searchResult") != -1){
                    item.removeAll();
                }
            });
        },
		//给默认加载矢量图形中添加或者移除图层
		addSublayers(viewId,layers){
			var view = maps[viewId],
			layer = view.map.findLayerById("testLayer");
			for(var i = 0;i < layers.length;i++){
				var sublayer = layer.findSublayerById(layers[i].layerId);
				if(sublayer){
					sublayer.visible = true;
				}else{
					sublayer = {
						id:layers[i].layerId,
						visible:true
					}
					layer.sublayers.push(sublayer);
				}
			}
		},
		removeSublayer(viewId,layers){
			var view = maps[viewId],
			layer = view.map.findLayerById("testLayer");
			 var items = layer.sublayers.items;
			for(var i = 0;i < layers.length;i++){
				 for (var k = 0;k<items.length;k++) {
				    if(layers[i].layerId == items[k].id){
				        items.splice(k, 1);
				        break;
				    }
				}
			}
			 layer.sublayers = items;
		},
		//设置图层透明度
		setLayerOpacity(viewId,layers,value){
			var view = maps[viewId],
			layer = view.map.findLayerById("testLayer");
			for(var i = 0;i < layers.length;i++){
				var sublayer = layer.findSublayerById(layers[i].layerId);
				if(sublayer){
					sublayer.opacity = value/100;
				}
			}
		}
    }
}