同时添加信息窗口、图例、比例尺和要素图层

Add InfoWindow, legend, scalebar and feature layer together

我正在尝试使用 JavaScript 在一个 html 页面中一起添加图例、双比例尺、要素层和信息 window。但是,当我可以添加比例尺、要素图层和信息window时,添加信息window javascript代码将导致我所有的功能消失。但是,当我尝试先输入信息 window,然后添加图例、比例尺和要素图层时,所有内容也都消失了。下面是我添加图例、要素图层和比例尺以及信息 window 的代码。除信息 window 外,一切正常,我不明白为什么。 JavaScript 中的代码放置有问题吗?我没有遵循特定的顺序?

<!DOCTYPE html>
<html>
<head>
    <!-- add in meta elements -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Mock Practical</title>
    <!-- reference styles -->
    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">
    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">
    <!-- reference arcGIS javascript -->
    <script src="http://js.arcgis.com/3.9/"></script>
    <style>
        html, body {
            height: 97%;
            width: 98%;
            margin: 1%;
        }

        #rightPane {
            width: 20%;
        }

        #legendPane {
            border: solid #97DCF2 1px;
        }
    </style>
    <!-- javascript -->
    <script>
     var map;
     require([
     "esri/map", "esri/dijit/InfoWindowLite",
     "esri/InfoTemplate", "esri/layers/FeatureLayer", "esri/dijit/Legend",
     "dojo/_base/array", "dojo/parser", "esri/dijit/Scalebar",
     "dijit/layout/BorderContainer", "dijit/layout/ContentPane",
    "dijit/layout/AccordionContainer",  "dojo/dom-construct", "dojo/domReady!"
     ], function(
     Map, InfoWindowLite, InfoTemplate, FeatureLayer, Legend,
     arrayUtils, parser,  Scalebar, domConstruct
     ) {
     parser.parse();
     map = new Map("map", {
     basemap:"topo",
     center: [-98.416, 39.781],
     zoom: 6
     });

      // scalebar
      var scalebar = new Scalebar({
          map: map,
          // "dual" displays both miles and kilmometers
          // "english" is the default, which displays miles
          // use "metric" for kilometers
          scalebarUnit: "dual", attachTo:"bottom-center"

        });

     // feature layer
    var featureLayer = new
     FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3", {
     mode: FeatureLayer.MODE_ONDEMAND,
     outFields:["STATE_NAME", "SUB_REGION", "STATE_ABBR"]
     });
     //map.addLayer(featureLayer);


     //add the legend
     map.on("layers-add-result", function (evt) {
     var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
     return {layer:layer.layer, title:layer.layer.name};
     });
     if (layerInfo.length > 0) {
     var legendDijit = new Legend({
     map: map,
     layerInfos: layerInfo
     }, "legendDiv");
     legendDijit.startup();
     }
     });
     map.addLayers([featureLayer]);


     var legendFeature = new
    FeatureLayer("http://www.onemap.sg/ArcGIS/rest/services/TOC/MapServer/6", {
     mode: FeatureLayer.MODE_ONDEMAND,
     outFields:["*"]
     });

     // infoWindow
     var infoWindow = new InfoWindowLite(null, domConstruct.create("div", null, null,
    map.root));
     infoWindow.startup();
     map.setInfoWindow(infoWindow);

     var template = new InfoTemplate();
    template.setTitle("<b>State name ${STATE_NAME} - State abbr ${STATE_ABBR}</b>");
    template.setContent("${SUB_REGION} is in district ${STATE_NAME}.");
    map.addLayer(featureLayer);
    map.infoWindow.resize(200,75);
     });

    </script>

</head>

<body class="claro">
    <div id="content" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:true" style="width: 100%; height: 100%; margin: 0;">
        <div id="rightPane" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'right'">
            <div data-dojo-type="dijit/layout/AccordionContainer">
                <div data-dojo-type="dijit/layout/ContentPane" id="legendPane" data-dojo-props="title:'Legend', selected:true">
                    <div id="legendDiv"></div>
                </div>
                <div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="title:'Pane 2'">
                    This pane could contain tools or additional content
                </div>
            </div>
        </div>
        <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" style="overflow:hidden;">

        </div>

    </div>
</body>
</html>

我在上面的代码片段中发现了一些小错误。以下是详情。

  1. 忘记在 domConstruct
  2. 之前添加 "BorderContainer, ContentPane, AccordionContainer,"
  3. 您没有将该信息模板对象添加到要素图层。

下面是工作代码:

<!DOCTYPE html>
<html>
<head>
    <!-- add in meta elements -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Mock Practical</title>
    <!-- reference styles -->
    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">
    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">
    <!-- reference arcGIS javascript -->
    <script src="http://js.arcgis.com/3.9/"></script>
    <style>
        html, body {
            height: 97%;
            width: 98%;
            margin: 1%;
        }

        #rightPane {
            width: 20%;
        }

        #legendPane {
            border: solid #97DCF2 1px;
        }
    </style>
    <!-- javascript -->
    <script>
        var map;
        require([
        "esri/map", "esri/dijit/InfoWindowLite",
        "esri/InfoTemplate", "esri/layers/FeatureLayer", "esri/dijit/Legend",
        "dojo/_base/array", "dojo/parser", "esri/dijit/Scalebar",
        "dijit/layout/BorderContainer", "dijit/layout/ContentPane",
       "dijit/layout/AccordionContainer", "dojo/dom-construct", "dojo/domReady!"
        ], function (
        Map, InfoWindowLite, InfoTemplate, FeatureLayer, Legend,
        arrayUtils, parser, Scalebar, BorderContainer, ContentPane, AccordionContainer, domConstruct
        ) {
            parser.parse();
            map = new Map("map", {
                basemap: "topo",
                center: [-98.416, 39.781],
                zoom: 6
            });

            // scalebar
            var scalebar = new Scalebar({
                map: map,
                // "dual" displays both miles and kilmometers
                // "english" is the default, which displays miles
                // use "metric" for kilometers
                scalebarUnit: "dual", attachTo: "bottom-center"

            });

            var template = new InfoTemplate("<b>State name ${STATE_NAME} - State abbr ${STATE_ABBR}</b>", "${SUB_REGION} is in district ${STATE_NAME}.");
            //template.setTitle("<b>State name ${STATE_NAME} - State abbr ${STATE_ABBR}</b>");
            //template.setContent("${SUB_REGION} is in district ${STATE_NAME}.");

          //  map.infoWindow.resize(200, 75);

            // feature layer
            var featureLayer = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3", {
                 mode: FeatureLayer.MODE_ONDEMAND,
                 outFields: ["STATE_NAME", "SUB_REGION", "STATE_ABBR"],
                 infoTemplate: template
             });
              map.addLayer(featureLayer);


            //add the legend
            map.on("layers-add-result", function (evt) {
                var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
                    return { layer: layer.layer, title: layer.layer.name };
                });
                if (layerInfo.length > 0) {
                    var legendDijit = new Legend({
                        map: map,
                        layerInfos: layerInfo
                    }, "legendDiv");
                    legendDijit.startup();
                }
            });
            map.addLayers([featureLayer]);


            var legendFeature = new FeatureLayer("http://www.onemap.sg/ArcGIS/rest/services/TOC/MapServer/6", {
               mode: FeatureLayer.MODE_ONDEMAND,
               outFields: ["*"]
           });

            // infoWindow
           // var infoWindow = new InfoWindowLite(null, domConstruct.create("div", null, null,
           //map.root));
           // infoWindow.startup();
           // map.setInfoWindow(infoWindow);


        });

    </script>

</head>

<body class="claro">
    <div id="content" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:true" style="width: 100%; height: 100%; margin: 0;">
        <div id="rightPane" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'right'">
            <div data-dojo-type="dijit/layout/AccordionContainer">
                <div data-dojo-type="dijit/layout/ContentPane" id="legendPane" data-dojo-props="title:'Legend', selected:true">
                    <div id="legendDiv"></div>
                </div>
                <div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="title:'Pane 2'">
                    This pane could contain tools or additional content
                </div>
            </div>
        </div>
        <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" style="overflow:hidden;">

        </div>

    </div>
</body>
</html>

实际上这是 dojo AMD 结构的标准...比如你需要多少个小部件,你必须按相同的顺序给它一个别名... 别名可以是您可以理解的任何 xyz,但是当您在应用程序中使用这些 dojo 小部件时,您必须使用这些别名来初始化该小部件。

来自 Dojo 站点:

“现代”Dojo 的基础是 require() 函数。它创建了一个 JavaScript 代码的闭包,并为它提供了完成工作所需的模块,因为 return 变量作为参数传递给函数。通常,第一个参数是模块 ID (MID) 数组,第二个参数是函数。在 require() 的闭包中,我们根据我们在参数中声明的变量名来引用模块。虽然我们可以随意调用模块,但有一些常用的约定,通常在 dojo Reference Guide.

中注明

现代 Dojo 仅表示 dojo AMD 或 dojo 格式 1.7 以上...

希望这对您有所帮助:)