ArcGIS API for JavaScript的CDN (https://js.arcgis.com/4.2/) 在中国访问实在不稳定,被墙的厉害。

本文描述如何在自己的服务器上部署ArcGIS API for JavaScript,以作开发之用。

1. 从ArcGIS for Devloper上下载ArcGIS API for JavaScript。

地址:https://developers.arcgis.com/downloads/

2. 将下载到的API文件解压后,部署到服务器。

我使用自己的static-host应用来托管静态文件,并设置路由为/static/arcgis。

有关static-host详情,查看github

部署完成后,就可以通过https://xzdbd.com/static/arcgis/js/4.2/init.js来访问JS API了。

3. 在正式使用前,需要修改init.js文件。

在init.js文件中搜索HOSTNAME_AND_PATH_TO_JSAPI,找到如下内容。

baseUrl:(location.protocol === 'file:' ? 'http:' : location.protocol) + '//' +
 "[HOSTNAME_AND_PATH_TO_JSAPI]dojo",hasCache:{"config-deferredInstrumentation":0,
 "config-selectorEngine":"acme","config-tlmSiblingOfDojo":1,"dojo-built":1,
 "dojo-has-api":1,"dojo-loader":1,"dojo-undef-api":0,dom:1,"host-browser":1},
 packages:[{location:".",name:"dojo"},{location:"../dijit",name:"dijit"},
 {location:"../dojox",name:"dojox"},{location:"../dgrid",
 main:"OnDemandGrid",name:"dgrid"},{location:"../dstore",main:...

将其中[HOSTNAME_AND_PATH_TO_JSAPI]修改为部署的API的URL,如:

xzdbd.com/static/arcgis/js/4.2/

然后指定URL Schema,我使用https,所以需要将默认的http改为https。

修改完成后,该段落内容如下:

baseUrl:(location.protocol === 'file:' ? 'https:' : location.protocol) + '//' +
 "xzdbd.com/statis/arcgis/js/4.2/dojo",hasCache:{"config-deferredInstrumentation":0,
 "config-selectorEngine":"acme","config-tlmSiblingOfDojo":1,"dojo-built":1,"dojo-has-api":1,
 "dojo-loader":1,"dojo-undef-api":0,dom:1,"host-browser":1},
 packages:[{location:".",name:"dojo"},{location:"../dijit",name:"dijit"},
 {location:"../dojox",name:"dojox"},{location:"../dgrid",
 main:"OnDemandGrid",name:"dgrid"},{location:"../dstore",main:...

4. 基本完成。但是当实际使用时,会遇到跨域请求文件的错误

'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

所以我们需要在文件请求响应的Header中加入”Access-Control-Allow-Origin”: “*”

Resonse Headers:

access-control-allow-origin:*
content-encoding:gzip
content-type:application/javascript
date:Wed, 11 Jan 2017 04:05:34 GMT
last-modified:Fri, 06 Jan 2017 08:40:07 GMT
server:nginx
status:200
vary:Accept-Encoding

本文链接: https://xzdbd.com/blog/how-to-deploy-arcgis-api-for-js参与评论»

–EOF–