2017-04-03 27 views
0

我有一個ArcGIS Online公共帳戶並將WebMap添加到我的網站。ArcGIS Online WebMap認證超時

我的ArcGIS Online WebMap看起來像這樣ESRI的樣本:LINK

而且我想我WebMap添加到我的網站,像這樣的ESRI的參考頁。你會看到頁面中心有一張地圖:LINK

我的網絡地圖很好地顯示在我的網頁上。當我訪問我的網頁時,我的WebMap會詢問我的ID和密碼。如果我輸入它,那麼它會顯示我的地圖。

但是,我的問題是,如果我移動到不同的頁面,然後回到地圖頁面,它會再次詢問。是否可以設置超時時間,以便每次訪問該頁面時都不必登錄?

回答

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 :("); 
       } 
      } 
     });