ArcGIS API for JavaScript 4.x 免登錄調用arcgis online私有服務

API keys | ArcGIS Developers

 

前言

 本來以爲普通用戶調用服務只能依靠登錄,仔細研究了一下可以通過key來實現免登錄調用服務。

背景

最近在做一個BIM結合GIS的Demo,先通過arcgis pro將.rvt文件配準到實際位置,然後打包成slpk文件,拖拽到arcgis online發佈出來,最後在前端加載。 

環境

 arcgis pro 3.0.0

Revit 2021

arcgis api for jacvascipt 4.24

Win10

問題

 01.免費版的online只能發佈私有服務,調用私有服務需要登錄arcgis online賬號

02. 自己發佈的服務無法刪除:

 03.續訂日期才能恢復服務

 

所謂的續訂服務的日期大概就是這個吧

 

解決方案

 既然只能發佈私有服務,那隻能使用api key來解決免登錄問題。

進入開發者儀盤表(DashboardAPI keys),發現有一個默認的api-key,可以使用這個key,但我覺得自己新建一個比較好。

 

方案一,使用現有默認key:

①單擊View Usage

 ②進入Overview,找到右下角的"Set content item scopes"

 ③選中需要綁定的服務,然後單擊右下角Add items

 ④結果

 

⑤如果需要,還可以限制訪問的域名

 方案二,新建api-key

①單擊左上角新增api key

 ②設置名稱和描述

 ③後面的步驟跟方案一相同了:

 

 

代碼部分:

const addBIM = (view) => {
  /**
   * apiKey可以在esriConfig設置,也可以在BuildingSceneLayer裏面設置
   */
  const apiKey="AAPK1457dc3f91554c6fbde4a85e9826f27bszcOiZPFXyTkT3DClwZbHFvPNvSPNffmWyEF2rfpCeVV5osoQxdS";//此key已經刪除一部分,僅作示例用
  // esriConfig.apiKey = apiKey;
  const buildingLayer = new BuildingSceneLayer({
    url: "https://tiles.arcgis.com/tiles/pDXnn9eULK5uzFWf/arcgis/rest/services/wjschoolrvt/SceneServer",
    title: "BIM圖層",
    apiKey:apiKey
  });
  view.map.layers.add(buildingLayer);
  view.goTo({
    center: [120.602, 31.145],
    heading: 10,
    zoom: 20,
    tilt: 45
  })
  view.popup.autoOpenEnabled = false;
  // Get the screen point from the view's click event
  view.on("click", function (event) {
    // Search for graphics at the clicked location. View events can be used
    // as screen locations as they expose an x,y coordinate that conforms
    // to the ScreenPoint definition.
    view.hitTest(event).then(function (response) {
      let result = response.results[0];

      if (result) {
        let lon = result.mapPoint.longitude;
        let lat = result.mapPoint.latitude;

        console.log("Hit graphic at (" + lon + ", " + lat + ")", result.graphic);
      } else {
        console.log("Did not hit any graphic");
      }
    });
  });
}
View Code

 

參考網址

01.Set location service scopes(設置api key的關聯的服務)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章