【记录】openlayers使用WebGLPointsLayer优化VectorLayer加载geojson大文件卡顿问题

生成 VectorSource

const pointSource = new VectorSource({
  url: url,
  format: new GeoJSON(),
});

VectorLayer 写法

const newlayer = new VectorLayer({
  source: pointSource,
  style: (feature) => {
    const type: string = feature.getProperties()[field];
    const color = mapFillColor(parseFloat(type)); // 颜色匹配函数

    return new Style({
      image: new CircleStyle({
        radius: 3, // 圆点半径
        fill: new Fill({ color: color }), // 填充颜色
        stroke: new Stroke({
          color: "#fff", // 边框颜色
          width: 0.5, // 边框宽度
        }),
      }),
    });
  },
});

WebGLPointsLayer 写法

const newWebGLlayer = new WebGLPointsLayer({
  source: pointSource,
  style: {
    'circle-radius': 3,
    "circle-fill-color": [
      "match",
      ["get", "type"],
      1,
      "rgb(139, 209, 0)",
      2,
      "rgb(255, 255, 0)",
      3,
      "rgb(255, 128, 0)",
      4,
      "rgb(255, 0, 0)",
      "rgb(255, 255, 255)",
    ],
    'circle-stroke-color': "rgb(255, 255, 255)",
  },
});

如果只需写点的填充颜色样式"circle-fill-color",也必须写点大小"circle-radius",否则会报错
其中match的语法是 [‘match’, input, match1, output1, match2, output2, …, defaultOutput]

相关链接:
openlayers>WebGLStyle:https://openlayers.org/en/latest/apidoc/module-ol_layer_WebGLTile.html#~Style
openlayers>ColorExpression:https://openlayers.org/en/latest/apidoc/module-ol_style_flat.html#~ColorExpression
openlayers>WebGLPointsLayer示例:https://openlayers.org/en/latest/examples/webgl-points-layer.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值