You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ArcGIS JS 3.23:如何为GraphicsLayer特定要素设置UniqueValueRenderer?

使用UniqueValueRenderer为ArcGIS JS API 3.x的GraphicsLayer设置渲染效果

首先明确回答你:ArcGIS JavaScript API 3.x版本完全支持给GraphicsLayer使用UniqueValueRenderer,这点和4.x的限制不同,你可以放心用。

下面给你一个完整的示例代码,展示如何实现这个需求:

require([
  "esri/map",
  "esri/layers/GraphicsLayer",
  "esri/graphic",
  "esri/symbols/SimpleMarkerSymbol",
  "esri/renderers/UniqueValueRenderer",
  "esri/Color",
  "dojo/domReady!"
], function(
  Map, GraphicsLayer, Graphic,
  SimpleMarkerSymbol, UniqueValueRenderer, Color
) {
  // 创建地图实例
  const map = new Map("map", {
    basemap: "streets",
    center: [-122.45, 37.75],
    zoom: 12
  });

  // 创建GraphicsLayer
  const graphicsLayer = new GraphicsLayer();
  map.addLayer(graphicsLayer);

  // 1. 创建UniqueValueRenderer,指定用来区分要素的属性字段
  const baseSymbol = new SimpleMarkerSymbol();
  const renderer = new UniqueValueRenderer(baseSymbol, "category");

  // 2. 为不同的属性值设置对应的符号
  renderer.addValue("restaurant", new SimpleMarkerSymbol({
    color: new Color([255, 0, 0]),
    size: 12,
    style: SimpleMarkerSymbol.STYLE_CIRCLE
  }));
  renderer.addValue("park", new SimpleMarkerSymbol({
    color: new Color([0, 255, 0]),
    size: 12,
    style: SimpleMarkerSymbol.STYLE_SQUARE
  }));
  renderer.addValue("shop", new SimpleMarkerSymbol({
    color: new Color([0, 0, 255]),
    size: 12,
    style: SimpleMarkerSymbol.STYLE_TRIANGLE
  }));

  // 3. 创建带属性的Graphic要素
  const restaurantGraphic = new Graphic(
    map.extent.getCenter(),
    null,
    { category: "restaurant" } // 这里的属性要和renderer指定的字段匹配
  );
  const parkGraphic = new Graphic(
    map.extent.getCenter().offset(0.02, 0.02),
    null,
    { category: "park" }
  );
  const shopGraphic = new Graphic(
    map.extent.getCenter().offset(-0.02, -0.02),
    null,
    { category: "shop" }
  );

  // 添加图形到图层
  graphicsLayer.add(restaurantGraphic);
  graphicsLayer.add(parkGraphic);
  graphicsLayer.add(shopGraphic);

  // 4. 给GraphicsLayer设置渲染器
  graphicsLayer.setRenderer(renderer);
});

关键说明:

  • 你需要确保每个Graphic对象的attributes里包含你在UniqueValueRenderer构造函数中指定的字段(示例里是category),渲染器会根据这个字段的值自动匹配对应的符号。
  • 如果你有部分图形不需要匹配规则,可以设置默认符号(就是构造UniqueValueRenderer时传入的baseSymbol),没有匹配到属性值的图形会使用这个默认符号。
  • 3.x的GraphicsLayer的setRenderer方法完全可以正常工作,这和4.x中该方法被移除的情况不同,你可以放心在3.23版本中使用。

内容的提问来源于stack exchange,提问作者Moshe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:47:09