# ArcGIS 4.x与3.x的常用API的区别
版本信息:ArcGIS API for JS 4.x
4.x和3.x的版本相比有了不小的变化,很多API的写法发生了变化,此文记录常用API的写法和用法区别。不断整理更新!📝
参考资料:
# 1.获取图层
// 3.x
map.getLayer("layerId")
// 4.x
map.findLayerById("layerId")
 1
2
3
4
5
2
3
4
5
# 2.移除图层
// 3.x
map.removeLayer(map.getLayer("layerId"))
// 4.x
map.remove(map.findLayerById("layerId"))
 1
2
3
4
5
2
3
4
5
# 3.添加图层
symbol样式可以在官方文档的esri/symbols中找到实例以及教程。👇
构造一个geometry(point)对象在第4部分可以找到。
// 3.x
require([
    'esri/layers/GraphicsLayer'
], function (GraphicsLayer) {
    const newLayer = new GraphicsLayer({id: "newLayer"});// 图层id
	map.addLayer(newLayer);
})
// 4.x
require([
    'esri/Graphic'
], function (Graphic) {
    const GeometryObject = {
        geometry: geometry,// geometry(point)对象
        symbol: symbol// symbol样式,可以是线、图片等
    }
    const layer = new Graphic(GeometryObject);
    map.add(layer);
})
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 4.获取地图中心点和比例尺
3.x版本用法请查ArcGIS 3.x常用方法整理。
  require([
      'esri/Map',
      'esri/views/MapView',
      "esri/geometry/Point"
  ], function (Map, MapView, Point) {
      const map = new Map({});
      const mapView = new MapView({
          container: 'map',
          map: map
      });
      ...
      const centerPoint = new Point({// 构造一个geometry(point)对象
          x: xValue,
          y: yValue,
          spatialReference: mapView.spatialReference
      });
      mapView.scale = 10000;// 设置比例尺
      mapView.center = centerPoint;// 设置中心点
      ...
      // 获取比例尺和中心点 直接从mpView拿就好了
      console.log(mapView.scale);
      console.log(mapView.center);
  })
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
🍗 有待补充...