如何知道 javascript require 标签的哪一部分添加我的函数?
How to know which part of the javascript require tag to add my function in?
您好,我正在尝试为我的地图添加定位按钮,但我不知道要在 require 标记中添加 javascript 代码的哪一部分。例如在 esri/dijit/Legend
之后还是什么?下面是我的代码。
<script>
require([
"dojo/parser",
"dojo/ready",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dojo/dom",
"esri/map",
"esri/urlUtils",
"esri/arcgis/utils",
"esri/dijit/Legend",
"esri/dijit/Scalebar",
"dojo/domReady!"
], function (
parser,
ready,
BorderContainer,
ContentPane,
dom,
Map,
urlUtils,
arcgisUtils,
Legend,
Scalebar
) {
ready(function () {
parser.parse();
//if accessing webmap from a portal outside of ArcGIS Online, uncomment and replace path with portal URL
//arcgisUtils.arcgisUrl = "http://pathto/portal/sharing/content/items";
arcgisUtils.createMap("7f975854312c4ca9a50aa5933c4a782e", "map").then(function (response) {
//update the app
dom.byId("title").innerHTML = response.itemInfo.item.title;
dom.byId("subtitle").innerHTML = response.itemInfo.item.snippet;
var map = response.map;
//add the scalebar
var scalebar = new Scalebar({
map: map,
scalebarUnit: "english"
});
//add the legend. Note that we use the utility method getLegendLayers to get
//the layers to display in the legend from the createMap response.
var legendLayers = arcgisUtils.getLegendLayers(response);
var legendDijit = new Legend({
map: map,
layerInfos: legendLayers
}, "legend");
legendDijit.startup();
});
});
});
</script>
您可以按任何顺序将项目添加到 require 数组中,但有两个注意事项:
- 您必须以相同的顺序在函数参数列表中添加模块 return 值
- 它必须位于您未在函数参数中包含 return 值的任何项目之前,除非您也不需要它的 return 值
所以,换句话说......不要把它放在 domReady 之后!因为函数参数中没有 return 值。比如放在ScaleBar和domReady之间,然后在函数参数中的ScaleBar后面加上return值:
require([
"dojo/parser",
"dojo/ready",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dojo/dom",
"esri/map",
"esri/urlUtils",
"esri/arcgis/utils",
"esri/dijit/Legend",
"esri/dijit/Scalebar",
"esri/dijit/LocateButton", <-- Here
"dojo/domReady!"
], function (
parser,
ready,
BorderContainer,
ContentPane,
dom,
Map,
urlUtils,
arcgisUtils,
Legend,
Scalebar,
LocateButton <-- Here
) {
但如前所述,顺序并不重要。您可以将它作为 require 数组中的第一项,只要它也是函数参数列表中的第一项。
您好,我正在尝试为我的地图添加定位按钮,但我不知道要在 require 标记中添加 javascript 代码的哪一部分。例如在 esri/dijit/Legend
之后还是什么?下面是我的代码。
<script>
require([
"dojo/parser",
"dojo/ready",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dojo/dom",
"esri/map",
"esri/urlUtils",
"esri/arcgis/utils",
"esri/dijit/Legend",
"esri/dijit/Scalebar",
"dojo/domReady!"
], function (
parser,
ready,
BorderContainer,
ContentPane,
dom,
Map,
urlUtils,
arcgisUtils,
Legend,
Scalebar
) {
ready(function () {
parser.parse();
//if accessing webmap from a portal outside of ArcGIS Online, uncomment and replace path with portal URL
//arcgisUtils.arcgisUrl = "http://pathto/portal/sharing/content/items";
arcgisUtils.createMap("7f975854312c4ca9a50aa5933c4a782e", "map").then(function (response) {
//update the app
dom.byId("title").innerHTML = response.itemInfo.item.title;
dom.byId("subtitle").innerHTML = response.itemInfo.item.snippet;
var map = response.map;
//add the scalebar
var scalebar = new Scalebar({
map: map,
scalebarUnit: "english"
});
//add the legend. Note that we use the utility method getLegendLayers to get
//the layers to display in the legend from the createMap response.
var legendLayers = arcgisUtils.getLegendLayers(response);
var legendDijit = new Legend({
map: map,
layerInfos: legendLayers
}, "legend");
legendDijit.startup();
});
});
});
</script>
您可以按任何顺序将项目添加到 require 数组中,但有两个注意事项:
- 您必须以相同的顺序在函数参数列表中添加模块 return 值
- 它必须位于您未在函数参数中包含 return 值的任何项目之前,除非您也不需要它的 return 值
所以,换句话说......不要把它放在 domReady 之后!因为函数参数中没有 return 值。比如放在ScaleBar和domReady之间,然后在函数参数中的ScaleBar后面加上return值:
require([
"dojo/parser",
"dojo/ready",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dojo/dom",
"esri/map",
"esri/urlUtils",
"esri/arcgis/utils",
"esri/dijit/Legend",
"esri/dijit/Scalebar",
"esri/dijit/LocateButton", <-- Here
"dojo/domReady!"
], function (
parser,
ready,
BorderContainer,
ContentPane,
dom,
Map,
urlUtils,
arcgisUtils,
Legend,
Scalebar,
LocateButton <-- Here
) {
但如前所述,顺序并不重要。您可以将它作为 require 数组中的第一项,只要它也是函数参数列表中的第一项。