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]]);
  }
相关推荐
LinXunFeng4 小时前
Obsidian - 使用 Share Note 分享笔记并自部署
前端·笔记·github
乘风gg8 小时前
为什么AI 时代来临,大部分人吃不到红利
前端·ai编程·claude
恋猫de小郭9 小时前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter
IT_陈寒9 小时前
Redis内存爆了,原来我漏掉了这个致命配置
前端·人工智能·后端
恋猫de小郭9 小时前
解读 Android 17 全新内存限制,有没有“豁免”后门?
android·前端·flutter
Hyyy10 小时前
理解LLM的基本工作原理:预训练、微调、推理的区别
前端
Gatlin11 小时前
前端逆向与反逆向:一场猫鼠游戏的底层逻辑与实战
前端
代码煮茶11 小时前
React 组件封装方法论 —— 以 Todo App 为例
javascript·react.js
Pedantic11 小时前
本地通知(Local Notifications)学习笔记
前端
任沫11 小时前
Agent之Function Call
javascript·人工智能·go