ArcGIS Online WebMap 身份验证超时

ArcGIS Online WebMap authentication timeout

我有一个 ArcGIS Online public 帐户并将 WebMap 添加到我的网站。

我的 ArcGIS Online WebMap 看起来像这个 ESRI 的示例:LINK

我正在尝试将我的 WebMap 添加到我的网站,就像这个 ESRI 的参考页面一样。您会看到页面中央有一张地图:LINK

我的WebMap在我的网页上显示的很好。当我访问我的网页时,我的 WebMap 会询问我的 ID 和密码。如果我输入它,它就会显示我的地图。

但是,我的问题是,如果我移动到不同的页面然后返回到地图页面,它会再次询问。是否可以设置超时,这样我每次访问该页面时都不必登录?

我问这个问题的原因是想知道是否有办法简化我的代码并在前端处理代码。

我研究了 ESRI 提供的 OAuth,最后我使用了 esri/IdentityManager。有使用 esri/IdentityManager 包的引用;但是没有示例代码可以将其与使用 arcgisUtils.createMap

的个人 WebMap 一起使用

所以这是我工作的示例代码:

 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/LayerList",
          "esri/graphic",
          "esri/symbols/PictureMarkerSymbol",
          "esri/symbols/TextSymbol",
          "esri/geometry/Point",
          "esri/dijit/Scalebar",
          "dojo/_base/unload",
          "dojo/cookie",
          "dojo/json",
          "esri/config",
          "esri/IdentityManager",
          "esri/layers/FeatureLayer",
          "dojo/domReady!"
        ], function (
          parser,
          ready,
          BorderContainer,
          ContentPane,
          dom,
          Map,
          urlUtils,
          arcgisUtils,
          Legend,
          LayerList,
          Graphic,
          PictureMarkerSymbol,
          TextSymbol,
          Point,
          Scalebar,
          baseUnload,
          cookie,
          JSON,
          esriConfig,
          esriId,
          FeatureLayer
        ) {

            var mapOptions = {
            basemap: "topo",
                    autoResize: true, // see http://forums.arcgis.com/threads/90825-Mobile-Sample-Fail
                    center: [currentPosition.lng, currentPosition.lat],
                    zoom: 15,
                    logo: false
        };

            // cookie/local storage name
            var cred = "esri_jsapi_id_manager_data";

            // store credentials/serverInfos before the page unloads
            baseUnload.addOnUnload(storeCredentials);

            // look for credentials in local storage
            loadCredentials();

            parser.parse();

            esriConfig.defaults.io.proxyUrl = "/proxy/";

            //Create a map based on an ArcGIS Online web map id
            arcgisUtils.createMap('PUT-YOUR-ESRI-KEY', "esriMapCanvas", { mapOptions: mapOptions }).then(function (response) {

                var map = response.map;

                // add a blue marker
                    var picSymbol = new PictureMarkerSymbol(
                            'http://static.arcgis.com/images/Symbols/Shapes/RedPin1LargeB.png', 50, 50);
                    var geometryPoint = new Point('SET YOUR LAT', 'SET YOUR LONG');
                    map.graphics.add(new Graphic(geometryPoint, picSymbol));

                //add the scalebar
                var scalebar = new Scalebar({
                    map: map,
                    scalebarUnit: "english"
                });

                //add the map layers
                var mapLayers = new LayerList({
                    map: map,
                    layers: arcgisUtils.getLayerList(response)
                }, "esriLayerList");
                mapLayers.startup();

                //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
                }, "esriLegend");
                legendDijit.startup();
            });

            function storeCredentials() {
                // make sure there are some credentials to persist
                if (esriId.credentials.length === 0) {
                    return;
                }

                // serialize the ID manager state to a string
                var idString = JSON.stringify(esriId.toJson());
                // store it client side
                if (supports_local_storage()) {
                    // use local storage
                    window.localStorage.setItem(cred, idString);
                    // console.log("wrote to local storage");
                }
                else {
                    // use a cookie
                    cookie(cred, idString, { expires: 1 });
                    // console.log("wrote a cookie :-/");
                }
            }

            function supports_local_storage() {
                try {
                    return "localStorage" in window && window["localStorage"] !== null;
                } catch (e) {
                    return false;
                }
            }

            function loadCredentials() {
                var idJson, idObject;

                if (supports_local_storage()) {
                    // read from local storage
                    idJson = window.localStorage.getItem(cred);
                }
                else {
                    // read from a cookie
                    idJson = cookie(cred);
                }

                if (idJson && idJson != "null" && idJson.length > 4) {
                    idObject = JSON.parse(idJson);
                    esriId.initialize(idObject);
                }
                else {
                    // console.log("didn't find anything to load :(");
                }
            }
        });