openLayers设置随比例尺而改变的样式

javascript 复制代码
this.#circleLayer = new VectorLayer({
    source: source,
    zIndex: 100,
    name: "seaFishingCircleLayer",
    style: function (feature, resolution) {
      const { fillColor, strokeColor, name, factorImg } = feature.getProperties();
      let featureStyle = [];
      if (factorImg.length > 0) {
         factorImg.forEach((factor, index) => {
           console.log(factor);
           featureStyle.push(
             new Style({
               image: new Icon({
                 src: "./static/shipArea/" + factor + "Factor.png",
                 anchorOrigin: "top-left",
                 size: [32, 32],
                 offset: [-18 * index, -22],
               }),
               geometry: (feature) => {
                 const geometry = feature.getGeometry();
                 const center = that.getCoordinatesByGeometry(geometry);
                 const coordinate = fromLonLat(center);
                 return new Point(coordinate);
               },
             })
           );
        );
      }
      featureStyle.push(
        new Style({
          fill: new Fill({
             color: fillColor,
          }),
          stroke: new Stroke({
            color: strokeColor,
            width: 1,
           }),
           text: new Text({
             text: resolution < 500 ? name : "",
             font: "500 12px MiSans",
             fill: new Fill({
               color: "#fff",
             }),
             stroke: new Stroke({
               color: "rgba(0,0,0,0.8)",
               width: 2,
             }),
             overflow: true,
           }),
         })
       );
       return featureStyle;
     },
   });
this.#map.addLayer(this.#circleLayer);

但是中间出了一个问题,那就是在设置offset[]属性的时候,由于本人设置Icon大小为14px,当我设置size为[14, 14]时,大概由于有两个Icon,盒子放不下导致两个Icon都没有显示。把size扩大到双倍Icon的宽即可。另外offset的设置也有要求,所以本人在想有没有像下面这个overflow的属性一样,可以设置元素超出图形仍然显示

javascript 复制代码
new Style({
  fill: new Fill({
    color: fillColor,
  }),
  stroke: new Stroke({
    color: strokeColor,
    width: 1,
  }),
  text: new Text({
    text: resolution < 500 ? name : "",
    font: "500 12px MiSans",
    fill: new Fill({
      color: "#fff",
    }),
    stroke: new Stroke({
      color: "rgba(0,0,0,0.8)",
      width: 2,
    }),
    overflow: true,
  }),
})

另附一段getCoordinatesByGeomotry()的代码。作用在于获取一个geometry的中心点

javascript 复制代码
getCoordinatesByGeometry(geometry) {
    const type = geometry.getType();
    let coordinates = null;
    if (type === "Polygon") {
      coordinates = geometry.getInteriorPoint().getCoordinates();
    } else {
      coordinates = geometry.getCoordinates();
    }
    // 部分coordinates会有第三个数组元素。
    return toLonLat([coordinates[0], coordinates[1]]);
  }
相关推荐
AC赳赳老秦14 分钟前
OpenClaw多平台部署:Windows+Linux跨系统协同,实现全场景覆盖
linux·服务器·前端·网络·windows·deepseek·openclaw
喜欢吃鱿鱼31 分钟前
DES加解密(附带解决转义问题)-VUE
开发语言·前端·javascript
愚者游世33 分钟前
variadic templates(可变参数模板)各版本异同
开发语言·c++·程序人生·面试
腹黑天蝎座34 分钟前
前端性能优化实战指南:从原理到落地的全方位解决方案
前端·性能优化·监控
忆往wu前38 分钟前
一文通透 Vue动态组件体系:插槽|数据监听|组件通信|动态切换|缓存—闭环
前端·面试
奇奇怪怪的问题40 分钟前
问题总结:关于封装axios问题,导致外部使用接口报错,无法进入error回调
前端·axios
Jenlybein40 分钟前
速学 VS Code 插件开发入门,客制化你的开发体验
前端·javascript·visual studio code
书到用时方恨少!1 小时前
Python 面向对象进阶:多态——同一个接口,千种面孔
开发语言·python·多态·面向对象
无忧.芙桃1 小时前
现代C++精讲之处理类型
开发语言·c++
黎梨梨梨_1 小时前
C++入门基础(下)(重载,引用,inline,nullptr)
开发语言·c++·算法