MapManager.js 17 KB
define("esri/config esri/core/urlUtils dojo/_base/declare lib/BaseWidget dojo/_base/lang dojo/_base/array dojo/dom dojo/on dojo/topic dojo/cookie dojo/Deferred dojo/_base/html dojo/dom-class esri/Map esri/views/MapView esri/layers/Layer esri/layers/TileLayer esri/layers/VectorTileLayer esri/layers/FeatureLayer esri/layers/MapImageLayer esri/widgets/Legend esri/geometry/Extent esri/geometry/SpatialReference dojo/request/xhr dojo/promise/all esri/widgets/support/widget".split(" "), function (esriConfig, urlUtils, l, m, d, f, y, z, e, n, A, B, C, p, q, D, r, t, u, mapImageLayer,Legend, v, E, w, x, wu) {

	//arcgis跨域调取地图代理服务设置
    var url = "http://10.7.120.165/onemap/proxy/proxy.ashx?http://172.16.128.165:6080/ArcGIS/rest/services/basicmapservice/MapServer?account=xxzxweb_111111&f=json";
    $.ajax({
        url: url,
        timeout: 500,
        success: function (data) {
            esriConfig.request.corsEnabledServers.push("http://10.7.120.8/onemap/proxy/proxy.ashx");
            urlUtils.addProxyRule({
                urlPrefix: "http://172.16.128.165:6080/",
                proxyUrl: url
            });
            window.url=url;
        },
        error:function(error){
            esriConfig.request.corsEnabledServers.push("http://172.16.128.165/onemap/proxy/proxy.ashx");
            urlUtils.addProxyRule({
                urlPrefix: "http://172.16.128.165:6080/",
                proxyUrl: "http://172.16.128.165/onemap/proxy/proxy.ashx?http://172.16.128.165:6080/ArcGIS/rest/services/basicmapservice/MapServer?account=xxzxweb_111111&f=json"
            });
            url="http://172.16.128.165/onemap/proxy/proxy.ashx?http://172.16.128.165:6080/ArcGIS/rest/services/basicmapservice/MapServer?account=xxzxweb_111111&f=json"
            window.url=url;
        }
    });

	esriConfig.request.corsDetection=true;
    var g = null,
        h;
    h = l(m, {
        map: null,
        legend: null,
        appConfig: null,
        extent: null,
        ping0: 0,
        layerconfig: "./config/layer.json",
        layerattr: "./config/attribute.json",
        legendurl: "./config/legend.json",
        LoadedLayers: [],
        constructor: function (a) {
            this.appConfig = a || "";
            this.listenBuilderEvents();
            window.changeBaseLayer = d.hitch(this, this.changeBaseLayer);
            a != "" && (window.ONEMAP_SERVER = this.httpIndexOf(a.hosts.serverhost));
        },
        listenBuilderEvents: function () {
            e.subscribe("builder/mapReload", d.hitch(this, this._onMapReload));
            e.subscribe("addLayer", d.hitch(this, this.LoadLayer));
            e.subscribe("loadBaseMapLayers", d.hitch(this, this.loadBaseMapLayers));
            e.subscribe("visibleLayer", d.hitch(this, this.visibleLayer));
            e.subscribe("getLayerInfos", d.hitch(this, this.getLayerInfos));
            e.subscribe("openSplitMap", d.hitch(this, this.openSplitMap));
            e.subscribe("setSplitMapLayers", d.hitch(this, this.setSplitMapLayers))
        },
        initMap: function () {
            isServer_SET ? this.requestDefaultByUrlAndParam(this.getRequestUrlByLabel("configbase"), JSON.stringify({
                name: "main_config,attrs"
            })).then(d.hitch(this, function (a) {
                0 == a.status ? (a = JSON.parse(a.result.config[0].content), this.appConfig.map = a.map, this.appConfig.map.basemaps[0].visible = !0, this.appConfig.map.basemaps[1].visible = !1, this.LoadMap()) : console.log(a)
            }))/* : netServer && "admin" != n("username") ? this.requestDefaultByUrlAndParam(this.getRequestUrlByLabel("configbase"), JSON.stringify({
                name: "main_config,attrs,layers,legend"
            })).then(d.hitch(this, function (a) {
                if (0 == a.status) {
                    var b = JSON.parse(a.result.config[0].content);
                    this.appConfig.map = b.map;
                    this.appConfig.map.basemaps[0].visible = !0;
                    this.appConfig.map.basemaps[1].visible = !1;
                    a.result.attrs && (this.attrs = a.result.attrs ? a.result.attrs : []);
                    a.result.layers && (this.layers = a.result.layers ? a.result.layers : []);
                    a.result.legend && (this.legends = a.result.legend ? a.result.legend : []);
                    this.LoadMap()
                } else console.log(a)
            })) */: x([this._LoadLayerInfos(this.layerconfig), this._LoadLayerInfos(this.layerattr), this._LoadLayerInfos(this.legendurl)]).then(d.hitch(this, function (a) {
                this.layers = a[0].layers;
                this.attrs = a[1].attrs;
                this.legends = a[2].legends;
                this.LoadMap();
                // 原设计逻辑为 widget 的加载在 LoadMap 中进行
                // 略微延迟后,进行 widget 的加载
                this.LoadWidget();
            }), function (a) {
                console.log(a)
            })
        },
        LoadMap: function () {
            "" != this.appConfig && (this.extent = new v({
                xmin: this.appConfig.map.extent.xmin,
                ymin: this.appConfig.map.extent.ymin,
                xmax: this.appConfig.map.extent.xmax,
                ymax: this.appConfig.map.extent.ymax,
                spatialReference: new E(this.appConfig.map.extent.spatialReference)
            }), this.map = new p, this.view = new q({
                container: mapDivId,
                /*extent: this.extent,*/
                zoom: 2,
                map: this.map
            }), this.view.constraints = {
                rotationEnabled: false //ldq20180719禁止旋转
            }, this.legend1 = new Legend({
                view: this.view,
                layerInfos: []
            }),
            (this.view.extent = this.extent),
/*            window.AAA = {},
            Object.defineProperty(this, 'view', {
                get: function () {
                    return AAA;
                },
                set: function(value) {
                    window.AAA = value;
                    console.log('set:' + value);
                }
            }),*/

            this.loadBaseMapLayers(this.map),
            this.view.then(d.hitch(this, function () {
                e.publish("mapLoaded", this.map, this.view);
                window.mapview = this.view;
            })));

            this.view.ui.add(this.legend1, "bottom-left");
            this.legendRender(this.legend1, this.appConfig.map.basemaps);

        },
        /**
         * 重写Legend render 方法(arcgis_js_api/esri/widgets/Legend.js)
         * @param legendObject 图例对象
         * @param basemap   配置文件所配置的底图
         * hzw 20190424 隐藏服务图例需求
         */
        legendRender: function (legendObject, basemap) {
            legendObject.render = function () {
                var a = legendObject,
                    e = a.activeLayerInfos,
                    b = "esri-legend esri-widget",
                    e = e && e.toArray().map(function (b) {
                        // 如果是底图,则清空所有图例
                        window.mapview && window.mapview.allLayerViews &&
                        mapview.allLayerViews.toArray().map(function (m) {
                            m.layer && Array.isArray(basemap) && basemap.forEach( function (value) {
                                if (value.url == m.layer.url && b.title == m.layer.title) {
                                    b.legendElements = [];
                                }
                            })
                        });
                        // 去掉 ‘2018违法建设图斑’ 服务的图例
                        if(b.title == "2018违法建设图斑") {
                            b.legendElements = [];
                        }
                        // ---------------------
                        return a._renderLegendForLayer(b)
                    }).filter(function (a) {
                        return !!a
                    });
                return wu.tsx("div", {class: b},
                        wu.tsx("div", { class: "esri-legend__top", style: "height: 42px;background-color:#F8F8F8"},
                            wu.tsx("span", { style: "width:30px;margin-left:10%;margin-top:15px;float:left;color:black;"}, "图例"),
                                wu.tsx("span", { id: "esri-legend__close", class: "esri-popup__icon esri-icon-close", onclick: "legendClose()", style: "margin-right:10%;margin-top:13px;float:right;cursor: pointer;"},
                                    wu.tsx("img", { src: "images/x.png"}))),
                                         wu.tsx("div", { class: "esri-legend__coot" }, e));
            }
        },

        LoadWidget: function () {
            e.publish("loadwidget", this.map, this.view);
        },
        loadBaseMapLayers: function (a) {
            f.forEach(this.appConfig.map.basemaps, d.hitch(this, function (b, c) {
                this._loadBaseLayers(b, a);
            }));
            f.forEach(this.appConfig.map.operationallayers, d.hitch(this, function (b, c) {
                this._loadBaseLayers(b, a)
            }))
        },
        checkIsBaseLayer: function (a) {
            var b = !1;
            f.forEach(this.appConfig.map.basemaps, d.hitch(this, function (c, d) {
                c.key == a && (b = !0)
            }));
            f.forEach(this.appConfig.map.operationallayers, d.hitch(this, function (c, d) {
                c.key == a && (b = !0)
            }));
            return b
        },
        getLayerInfos: function (a, b) {
            a[b] = this[b]
        },
        visibleLayer: function (a, b) {
            try {
                if ("string" === typeof a) {
                    var c = this.map.findLayerById(a);
                    "undefined" === typeof c ? b && (e.publish("layer-change"), this.LoadLayer(a)) : ((c.visible = b) || this.checkIsBaseLayer(a) || this.map.remove(this.map.findLayerById(a)), e.publish("layer-change"))
                }
            } catch (k) {
                isDebug_L && (console.log(a + " visible:" + b + "error:"), console.log(k))
            }
        },
        _loadBaseLayers: function (a, b) {
        	var that=this;
            try {
                if ("undefined" === typeof a || "undefined" === typeof a.url || "" === a.url) return 0;
                var c = this._createlayer(a);
                "string" === typeof a.label && (c.name = a.label);
                "string" === typeof a.key && (c.id = a.key);
                "boolean" === typeof a.visible && (c.visible = a.visible);
                "number" === typeof a.opacity && (c.opacity = a.opacity);
                null != c && b && b.add(c);

				var url = a.url;

				var showSuccessMap = function (status) {
                    var _visible = true;
                    var basemaps = that.appConfig.map.basemaps;

                    window.baseMapsCase = window['baseMapsCase'] || {};
                    baseMapsCase.success || (baseMapsCase.success = []);
                    status == "success" && baseMapsCase.success.push(c);
                    baseMapsCase.error || (baseMapsCase.error = []);
                    status == "error" && baseMapsCase.error.push(c);

                    var size = baseMapsCase.success.length + baseMapsCase.error.length;
                    if (Array.isArray(basemaps) && basemaps.length == size) {
                        baseMapsCase.error.forEach( function (value) {
                            value.visible = false;
                        })
                        baseMapsCase.success.forEach( function (value) {
                            value.visible == true && (_visible = false);
                        });
                        if (_visible && typeof baseMapsCase.success[0] == "object") {
                             baseMapsCase.success[0].visible = true;
                        }
                    }
                };

				$.ajax({
					url: url,
		            timeout:4000,
		            success: function (data) {
                        showSuccessMap("success");
		            },
		            error:function(error){
                        showSuccessMap("error");
		            }
           		});
            } catch (k) {
            }
        },
        LoadLayer: function (a, b) {
            try {
                "string" === typeof a && f.forEach(this.layers, d.hitch(this, function (c, d) {
                    a == c.key && "undefined" !== typeof c && "undefined" !== typeof c.url && "" !== c.url && "undefined" === typeof(b ? b.findLayerById(a) : this.map.findLayerById(a)) && (d = this._createlayer(c), "string" === typeof c.label && (d.name = c.label), "string" === typeof c.key && (d.id = c.key), "boolean" === typeof c.visible && (d.visible = c.visible), "number" === typeof c.opacity && (d.opacity = c.opacity), null != d && ("undefined" === typeof b ? "undefined" !== typeof c.baselayer && c.baselayer ? (d.baselayer = c.baselayer, this.map.add(d, this.appConfig.map.basemaps.length)) : this.map.add(d) : b.add(d)))
                }))
            } catch (c) {
            }
        },
        _createlayer: function (a) {
            var b = this.GetLayerByKey(a.key),
                c;
            if (b) return b;
            c = this.getMapServerUrl(a.url);

            "dynamic" == a.type ? (b = new mapImageLayer({url: c})) :
                "tiled" == a.type ? b = new r(c) : "vector" == a.type ? (b = new t({
                    url: c
                }), b.title = a.label) : "feature" == a.type && (b = new u(c, {
                        outFields: ["*"]
                    }), "string" === typeof a.label && (b.label = a.label));

            b.always(d.hitch(this, function () {
                isDebug_L && console.log(a.label + "(" + a.key + "):" + b.loadStatus);
                "failed" == b.loadStatus && (isDebug_L && console.log(a.label + "(" + a.key + ") error:"), isDebug_L && console.log(b.loadError));
                "loaded" == b.loadStatus && this.AddLoadedLayers(b)
            }));
            return b
        },


       ReLoadLayer: function(a){
        	a.url="http://10.7.120.30:6080/arcgis/rest/services/遥感数据/2018西安影像/MapServer";
        	this.map.remove(this.map.findLayerById("dzdt"));
        	this.map.remove(this.map.findLayerById("yxdt"));
        	this.map.remove(this.map.findLayerById("dxfandshplayer"));
        	this._loadBaseLayers(a, this.map);
        	var key="3G_GH_DLHX";
        	this.LoadLayer(key, this.map);
        	return 1;

        },
        _LoadLayerInfos: function (a) {
            return w(a, {
                handleAs: "json"
            })
        },
        getMapServerUrl: function (a) {
            return -1 != a.indexOf("http://") ? a : "http://" + this.appConfig.hosts.mapserverhost + "/" + a
        },
        _onMapReload: function () {
        },
        openSplitMap: function (a) {
            a ? ($("#map .esri-zoom").hide(), $("#map .split-title").show()) : ($("#map .esri-zoom").show(), $("#map .split-title").hide(), this.clearMapBaseLayer())
        },
        setSplitMapLayers: function (a) {
            f.forEach(a, d.hitch(this, function (a, c) {
                f.forEach(a.rounds, d.hitch(this, function (b, c) {
                    "nnsz-splitscreen-round-selected" == b.class && "1" == b.name && (this.selectedLayer = a, this.selectedRound = b)
                }))
            }));
            this.clearMapBaseLayer();
            this.selectedLayer && ((a = this.map.findLayerById(this.selectedLayer.key)) ? a.visible = !0 : e.publish("addLayer", this.selectedLayer.key, this.map), $("#map .split-title").children("span").html(this.selectedLayer.parent + "_" + this.selectedLayer.label))
        },
        clearMapBaseLayer: function () {
            this.map.allLayers.forEach(d.hitch(this, function (a, b) {
                if (a) {
                    a.type && "graphics" != a.type && (a.visible = !1);
                    a.visible || this.checkIsBaseLayer(a.id) || this.map.remove(this.map.findLayerById(a.id))
                }
            }));
            f.forEach(this.appConfig.map.basemaps, d.hitch(this, function (a, b) {
                (b = this.map.findLayerById(a.key)) && a.visible && (b.visible = a.visible)
            }))
        },
        AddLoadedLayers: function (a) {
            f.forEach(this.LoadedLayers, d.hitch(this, function (b, c) {
                b.id == a.id && !1
            }));
            this.LoadedLayers.push(a)
        },
        GetLayerByKey: function (a) {
            f.forEach(this.LoadedLayers, d.hitch(this, function (b, c) {
                b.id == a && 0 == b
            }));
            return !1
        },
        changeBaseLayer: function (a) {
            this.map.remove(this.map.findLayerById("dzdt"));
            this.map.remove(this.map.findLayerById("yxdt"));
            "dzdt" == a.key ? this.appConfig.map.basemaps[0] = a : "yxdt" == a.key && (this.appConfig.map.basemaps[1] = a);
            this.loadBaseMapLayers(this.map);
            e.publish("changeBaseLayer", a)
        }
    });

    h.getInstance = function (a) {
        null === g ? g = new h(a) : g.appConfig = a;
        return g
    };
    return h
});