ArcGIS中自定义图层的legend

526 阅读1分钟

最近在做地图服务的开发,有一个小问题,很多时候你需要自定义图层,因为很多图层相关的操作,在你引用的第三方图层中是无法实现的,比如 绘制、计算、包括展示你需要的气泡窗等等交互,都需要在自定义的图层中来实现。

在ArcGIS的官方文档中,并没有给出相对应的自定义图层中的复用组件信息,比如legend developers.arcgis.com/javascript/…

我们要在自定义图层中使用API定义好的组件,就要遵从它的格式及样式,比如这样

let myRender = {
    type: "unique-value",
    field: "currType",
    defaultSymbol: { type: "simple-fill" },
    uniqueValueInfos: [
        {
           value: "North",
           symbol: {
              type: "simple-marker",
              color: [226, 119, 40],
              outline: {
                color: [255, 255, 255],
                width: 2
              }
            }
        },
        {
           value: "East",
           symbol: {
               type: "simple-marker",
               color: [146, 23, 142],
               outline: {
                 color: [255, 255, 255],
                 width: 2
               }
            }
        }
    ]
}
let myLayer = new FeatureLayer({
    source:[],
    objectIdField: "ObjectID",
    geometryType: "point",
    outFields:['*'],
    fields:[
       {
           name: "ObjectID",
           alias: "ObjectID",
           type: "oid"
       },
       {
           name: "DEV_ID",
           alias: "DEV_ID",
           type: "string"
       },
       {
           name: "currType",
           alias: "currType",
           type: "string"
       }
    ],
    renderer: myRender
})
let legend = new Legend({
    view: view,
    layerInfos: [
      {
        layer: myLayer,
        title: "my legend"
       }
     ]
});
map.add(myLayer)
view.ui.add(legend, "bottom-right");

有一点要注意,不论自定义的图层内的几何信息是有多个样式还是单个样式,都要按照多个样式去写,否则图例无法正常显示的。