ArcGIS Online WebMap身份验证超时

时间:2017-04-03 22:33:57

标签: esri arcgis-js-api

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

我的ArcGIS Online WebMap看起来像这个ESRI的样本:LINK

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

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

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

1 个答案:

答案 0 :(得分:1)

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

我研究了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 :(");
                }
            }
        });