ArcGIS API for JavaScript 学习笔记(二)
Dojo总是让人很无奈,有时候,一些功能需要跨文件去写,这边提一些图层保存、公共引用的小知识。
如何屏蔽require?
一些常用函数,每写一个功能就去引用一次,真的很恶心。
设置_g.esri={.....}
1 var _g = {}; 2 3 require([ 4 "esri/layers/TileLayer", 5 "esri/layers/FeatureLayer", 6 "esri/views/SceneView", 7 "esri/WebScene", 8 ], function ( TileLayer, FeatureLayer, SceneView, WebScene) { 9 //-------------------------------------------------------- 10 // 公共引用部分 11 //-------------------------------------------------------- 12 _g.esri = { 13 FeatureLayer: FeatureLayer, 14 TileLayer: TileLayer, 15 SceneView: SceneView 16 }; 17 18 //-------------------------------------------------------- 19 // map init 20 //-------------------------------------------------------- 21 22 _g.scene = new WebScene({ 23 portalItem: { 24 id: "24e......" 25 } 26 }); 27 28 _g.view = new SceneView({ 29 map: _g.scene, 30 container: "viewDiv" 31 }); 32 33 34 });
获取
1 var tlayer = new _g.esri.TileLayer({ 2 url:'xxxxxx', 3 opacity:0.5 4 }) ; 5 6 var flayer = new _g.esri.FeatureLayer({ 7 url:'xxxxxx', 8 opacity:0.5 9 }) ;
如何将图层统一管理?
这里用到了es6的语法,实现了类似于.net的Dictionary的数组功能
1 _g.layers = new Map(); 2 3 //添加 4 var ajLayer = _g.esri.FeatureLayer({ 5 url: "xxxx", 6 outFields: ["*"] 7 }); 8 _g.layers.set('案件分布图层', ajLayer); 9 10 //获取 11 _g.layers.get('案件分布图层'); 12 13 //判断是否存在 14 _g.layers.has('案件分布图层'); 15 16 //移出 17 _g.layers.delete('案件分布图层');
图层的Renderer可以单独封装
1 function createRenderer(){ 2 var opacityStops = [ 3 { 4 opacity: 1, 5 value: 2018 6 }, 7 { 8 opacity: 0, 9 value: 2001 10 } 11 ]; 12 13 return { 14 type: "simple", 15 symbol: { 16 type: "simple-fill", 17 color: "rgb(0, 0, 0)", 18 outline: null 19 } 20 } 21 } 22 23 //引用 24 layers.get('xxx').renderer = createRenderer();