InfoWindowLite 没有出现

InfoWindowLite not appearing

我正在尝试在我的地图上实现 InfoWindowLite 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>Map</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.infoWindow.resize(200,75);
     });

      function init() {
     dojo.connect(map,"onLoad", function(map) {map.infoWindow.resize(200, 90);} );
     dojo.connect(map, "onClick", addPoint);
     }
     function addPoint(evt) {
     map.infoWindow.setTitle("Coordinates");
     map.infoWindow.setContent("lat/lon : " + evt.mapPoint.y + ", " + evt.mapPoint.x);
     map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint));
     }
     dojo.addOnLoad(init);
    </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>

我在想,也许代码放置的顺序是它没有出现的原因。但是当我把//infoWindow下的代码移到//legend等其他功能之前时,当我运行它在chrome中时,其他功能将不会显示。

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

你忘记在 domConstruct 之前添加 "BorderContainer, ContentPane, AccordionContainer,"。

下面是工作代码:

<!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>Map</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"

        });

     // 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.infoWindow.resize(200,75);
     });

      function init() {
     dojo.connect(map,"onLoad", function(map) {map.infoWindow.resize(200, 90);} );
     dojo.connect(map, "onClick", addPoint);
     }
     function addPoint(evt) {
     map.infoWindow.setTitle("Coordinates");
     map.infoWindow.setContent("lat/lon : " + evt.mapPoint.y + ", " + evt.mapPoint.x);
     map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint));
     }
     dojo.addOnLoad(init);
    </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>

这将在您单击地图时显示信息弹出窗口。

希望这对您有所帮助:)