我正在尝试使用JavaScript在一个html页面中一起添加图例,双缩放栏,要素图层和信息窗口。但是,当我可以添加比例尺,要素图层和信息窗口时,添加信息窗口javascript代码将导致我的所有功能都消失。但是,当我尝试首先放入信息窗口然后添加图例,比例尺和要素图层时,一切都会消失。下面是我添加图例,要素图层和比例尺以及信息窗口的代码。一切都在工作,除了信息窗口,我无法弄清楚为什么。就像在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>
答案 0 :(得分:0)
我在上面的代码段中发现了一些小错误。以下是详细信息。
以下是工作代码:
Bundle
答案 1 :(得分:0)
实际上这是dojo AMD结构的标准......就像你需要多少个小部件一样,你必须以相同的顺序给它一个别名... 别名可能是您可以理解的任何xyz,但是当您在应用程序中使用这些dojo小部件时,您必须使用这些别名来初始化该小部件。
来自Dojo网站:
&#34;现代&#34;的基础Dojo是require()函数。它创建了一个JavaScript代码的闭包,并为它提供了完成工作所需的模块,因为返回变量作为参数传递给函数。通常,第一个参数是模块ID(MID)的数组,第二个参数是函数。在require()的闭包内,我们根据我们在参数中声明的变量名引用模块。虽然我们可以调用任何模块,但有一些常用的约定被使用,并且通常在dojo Reference Guide.
中注明。现代Dojo意味着简单的dojo AMD或dojo格式1.7以后......
希望这会对你有所帮助:)。