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]]);
  }
相关推荐
demi_meng1 小时前
reactNative 遇到的问题记录
javascript·react native·react.js
QX_hao1 小时前
【Go】--map和struct数据类型
开发语言·后端·golang
你好,我叫C小白1 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
千码君20162 小时前
React Native:从react的解构看编程众多语言中的解构
java·javascript·python·react native·react.js·解包·解构
Evand J3 小时前
【MATLAB例程】基于USBL和DVL的线性回归误差补偿,对USBL和DVL导航数据进行相互补偿,提高定位精度,附代码下载链接
开发语言·matlab·线性回归·水下定位·usbl·dvl
lijun_xiao20094 小时前
前端最新Vue2+Vue3基础入门到实战项目全套教程
前端
爱喝白开水a4 小时前
LangChain 基础系列之 Prompt 工程详解:从设计原理到实战模板_langchain prompt
开发语言·数据库·人工智能·python·langchain·prompt·知识图谱
Neverfadeaway4 小时前
【C语言】深入理解函数指针数组应用(4)
c语言·开发语言·算法·回调函数·转移表·c语言实现计算器
90后的晨仔4 小时前
Pinia 状态管理原理与实战全解析
前端·vue.js
武子康4 小时前
Java-152 深入浅出 MongoDB 索引详解 从 MongoDB B-树 到 MySQL B+树 索引机制、数据结构与应用场景的全面对比分析
java·开发语言·数据库·sql·mongodb·性能优化·nosql