ArcGIS API for JavaScript開發的首要步驟就是引入ArcGIS想關的樣式文件及開發包,對于此樣式文件及開發包的引用有兩種形式:在線引用和離線加載。對于一般的示例程序編寫,我們只需簡單的引入在線的開發包即可,但是在自己開發過程中可能會有引入離線開發包的需求,所以本教程將介紹如何本地部署API文件,具體操作如下:
1 打開“https://developers.arcgis.com/downloads/”網址下載相應版本的API,如下圖所示:
2 解壓下載好的API文件,然后將相應版本號的文件夾移動到服務器中(IIS或tomcat),本例是將3.24文件移動到IIS,如圖:
3 分別將文件夾中的“init.js”及“dojo/dojo.js”文件中的“[HOSTNAME_AND_PATH_TO_JSAPI]”修改為在本地的部署路徑“localhost/3.24”,如下圖所示:
4 在瀏覽器分別輸入“localhost/3.24/init.js”和“localhost/3.24/esri/css/esri.css”來驗證是否配置成功,有如下信息就說明成功:
5 此時文件本地部署成功后,我們就可以引用離線文件來創建地圖了,具體的代碼如下:
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
- <title>Simple Map</title>
- <link rel="stylesheet" href="http://localhost/3.25/esri/css/esri.css">
- <style>
- html, body, #map {
- height: 100%;
- margin: 0;
- padding: 0;
- }
- </style>
- <script src="http://localhost/3.25/init.js"></script>
- <script>
- var map;
- require(["esri/map", "dojo/domReady!"], function(Map) {
- map = new Map("map", {
- basemap: "topo", //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
- center: [-122.45, 37.75], // longitude, latitude
- zoom: 13
- });
- });
- </script>
- </head>
- <body>
- <div id="map"></div>
- </body>
- </html>
6 結果如圖: