【问题标题】:How to know which part of the javascript require tag to add my function in?如何知道 javascript 的哪一部分需要标签来添加我的功能?
【发布时间】:2015-10-12 19:30:27
【问题描述】:

您好,我正在尝试为我的地图添加一个定位按钮,但我不知道要在 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>

【问题讨论】:

    标签: javascript html arcgis esri arcgis-js-api


    【解决方案1】:

    您可以按任意顺序将项目添加到 require 数组中,但有两个注意事项:

    1. 必须在函数参数列表中按相同顺序添加模块返回值
    2. 它必须放在您没有在函数参数中包含返回值的任何项之前,除非您也不需要它的返回值

    所以,换句话说……不要把它放在 domReady 之后!因为函数参数中没有 this 的返回值。比如把它放在ScaleBar和domReady之间,然后在函数参数中ScaleBar后面加上返回值:

    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 数组中的第一项,只要它也是函数参数列表中的第一项。

    【讨论】:

      猜你喜欢
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      相关资源
      最近更新 更多