Blame view

src/main/webapp/js/map/initMap.js 7.06 KB
caiyongsong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
$(function () {
    console.log("监测评估");
    // 添加css
    var ele = document.createElement("link");
    ele.setAttribute("rel", "stylesheet");
    ele.setAttribute("href", '/frontweb/js/arcgis_js_api/esri/css/main.css');
    document.body.appendChild(ele)

    $.getScript('/frontweb/js/arcgis_js_api/init.js', function () {
        // 插入地图绑定元素
        var mapEle = document.getElementById('map')
        if (mapEle) {
            mapEle.setAttribute("style", "width: 100%;height: 100%;position: absolute;left: 0;right: 0;top: 0;bottom: 0;z-index: 1;");
        } else {
            var ele = document.createElement("div");
            ele.setAttribute('id', 'map')
            ele.setAttribute("style", "width: 100%;height: 100%;position: absolute;left: 0;right: 0;top: 0;bottom: 0;z-index: 1;");
            document.body.appendChild(ele)
        }

        // iframe 存在
        require = window.require || window.parent.require
        var CONF_FRONT_SERVERURL = window.CONF_FRONT_SERVERURL || window.parent.CONF_FRONT_SERVERURL

        // let map = window.mapInstance || window.parent.mapInstance
        // let graphicsLayer = window.graphicsLayerInstance || window.parent.graphicsLayerInstance
        // let view = window.mapViewInstance || window.parent.mapViewInstance

        var mapGeom = null


        require([
            "esri/Map",
            "esri/Basemap",
            "esri/views/MapView",
            "esri/layers/FeatureLayer",
            "esri/layers/TileLayer",
            "esri/geometry/Extent",
            // "esri/views/2d/draw/Draw",
            "esri/Graphic",
            "esri/geometry/Polyline",
            "esri/geometry/Polygon",
            // "esri/geometry/Point",
            "esri/layers/GraphicsLayer",
            "esri/layers/MapImageLayer",
            // "esri/symbols/SimpleLineSymbol",
            // "esri/symbols/SimpleFillSymbol",
            // "esri/symbols/TextSymbol",
            // "esri/Color",
            // "esri/widgets/Legend",
            // "esri/widgets/ScaleBar",
            // "esri/widgets/Search",
            // "esri/widgets/TimeSlider",
            "esri/PopupTemplate",
            "dojo/domReady!"
        ], function (Map, Basemap, MapView, FeatureLayer, TileLayer, Extent, Graphic,
                     Polyline, Polygon, GraphicsLayer, MapImageLayer, PopupTemplate) {
            var map = new Map({
                basemap: new Basemap({
                    baseLayers: [new TileLayer({
                        url: 'http://10.7.120.44:6080/arcgis/rest/services/规划2000/电子地图/MapServer'
                    })]
                })
            });
            var view = new MapView({
                container: "map",
                map: map,
                // center: [108.948024, 34.263161], // longitude, latitude
                extent: new Extent({
                    xmin: 438417,
                    ymin: 3725140,
                    xmax: 688512,
                    ymax: 3872165,
                    spatialReference: 4545
                }),
                zoom: 1
            });
            view.ui.remove('zoom')//清除放大缩小按钮
            view.ui.remove('attribution')//清楚底部powered by ESRI

            // 添加影像地图
            // var yxMap = new TileLayer({
            //     url: 'http://10.6.144.134:6080/arcgis/rest/services/%E5%BD%B1%E5%83%8F%E5%BA%95%E5%9B%BE2000/%E8%A5%BF%E5%AE%89%E5%B8%824/MapServer',
            // });

            // 添加莲湖区地图
            // var graphicsLayer = new GraphicsLayer();
            // var qxjMap = new FeatureLayer({
            //     url: "http://10.6.144.134:6080/arcgis/rest/services/国土数据2000/区县面/MapServer/0"
            // })

            // map.add(yxMap);
            // map.add(qxjMap);

            var listArray = [];
            $.ajax({
                url: 'geojson/allData.json',
                type: 'GET',
                async: false,
                success: function (res) {
                    console.log(res, 'json获取成功')
                    // addPolygon(res);
                }
                // })
                // view.when(() => {
                //     console.log(123);
                //     // graphicsLayer.graphics.items[6].symbol = new SimpleFillSymbol({
                //     //     color: [118, 255, 0, 1],
                //     //     outline: { color: [118, 255, 0, 0.6], width: 3 },
                //     // });
                // });
                // // 获取rings path
                // function getRings(path) {
                //     let rings = []
                //     path.forEach((item) => {
                //         item.forEach((pathItem) => {
                //             pathItem.forEach((childPathItem) => {
                //                 rings.push(childPathItem)
                //             })
                //         })
                //     })
                //     return rings
                // }
                // // 增加片区
                // function addPolygon(data) {
                //     var fillSymbol = {
                //         type: "simple-fill", // autocasts as new SimpleFillSymbol()
                //         color: [227, 139, 79, 0.8],
            })
            //   }

            window.addEventListener('message', function (res) {
                const type = res.data.type
                if (type === 'xzqdm') {
                    const mapGeomData = JSON.parse(mapGeom)
                    let isToggle = false

                    for (let i = 0; i < mapGeomData.length; i++) {
                        let item = mapGeomData[i]
                        if ( item.xzqdm === res.data.data) {
                            if (item.symbol) {
                                isToggle = true
                                break
                            }
                            item.symbol = {
                                //   type: "simple-fill",
                                // color: "red",
                                outline: {
                                    // autocasts as new SimpleLineSymbol()
                                    color: [255, 255, 255],
                                    width: 1
                                }
                            }
                        };
                        data.forEach(item => {
                            const path = JSON.parse(item.geometry).coordinates;
                            const rings = getRings(path);
                            const polylineAtt = {
                                XZQMC: item.xzqmc,
                                FID: item.gid,
                                type: 'area'
                            };
                            const polygonGraphic = new Graphic({
                                geometry: new Polygon({ spatialReference: { wkid: 4545 }, rings: rings }),
                                symbol: fillSymbol
                            });
                            graphicsLayer.addMany([polygonGraphic]);

                        })
                    }
                };
            });


        })
    })

})