0
点赞
收藏
分享

微信扫一扫

使用openlayers访问ArcGIS Server发布的地图服务

暮晨夜雪 2021-09-21 阅读 63
随笔开源

1. 下载并安装Node.js

下载链接

2. 复制粘贴代码

2.1. 创建一个文件夹,名字随便起,但是不能有中文

2.2. 在刚刚这个文件夹里分别创建以下文件

  • package.json
  • index.html
  • index.js

2.3. 复制以下代码

package.json 文件内容:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --experimental-scope-hoisting --public-url . index.html"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ol": "^6.3.1",
    "parcel": "^1.12.4"
  }
}

index.html :

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Tiled ArcGIS MapServer</title>
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,URL"></script>
    <style>
      .map {
        width: 100vw;
        height:100vh;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script src="index.js"></script>
  </body>
</html>

index.js :

import 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import {OSM, TileArcGISRest} from 'ol/source';

// 这个替换成你自己的路径
let url = 'http://localhost:6080/arcgis/rest/services/%E4%BD%9C%E4%B8%9A3_%E4%B8%96%E7%95%8C%E5%9C%B0%E5%9B%BE/MapServer';

var layers = [
  new TileLayer({
    source: new TileArcGISRest({
      url: url
    })
  })
];
var map = new Map({
  layers: layers,
  target: 'map',
  view: new View({
    center: [-10967148, 3069099],
    zoom: 6
  })
});

3. 执行命令,进行打包

安装cnpm

npm i -g cnpm

安装依赖:

cnpm install

进行打包:

npm run build

4. 查看结果

打包好后,可以看到一个dist文件夹,里面有一个index.html文件,打开它看看。

举报

相关推荐

0 条评论