vue项目使用openlayers来添加地图标注,标注样式设置的简要模板

先把代码贴出来,注释以后有时间再写(需要留意一下这里图标的引入方式,函数内相同路径无法找到图片)

import sk from "../../assets/img/home/sk-active.png";
import hd from "../../assets/img/home/river-active.png";
  // 雨量站  状态:颜色标识 点
  switch (status) {
    case 0: status = '#a8eba8'
      break;
    case 1: status = '#69befe'
      break;
    case 2: status = '#ea29fb'
      break;
    case 3: status = '#ea29fb'
      break;
  }
  function style(names) {
    return new Style({
      image: names === 'yl' ? new Circle({
        radius: 5,
        fill: new Fill({
          color: '#05ab57',
        }),
        stroke: new Stroke({
          color: status,
          width: 16
        }),
      }) : new Icon({
        anchor: [0.5, 30],
        anchorOrigin: 'bottom-right',
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        scale: 0.45,
        src: names === 'sk' ? sk : hd,
      }),
      text: new Text({
        textAlign: "center", // 位置
        textBaseline: "center", // 基准线
        offsetX: 0,
        offsetY: 30,
        text: name,
        fill: new Fill({
          color: "#333",// 文本填充样式(即文字颜色)
        }),
        stroke: new Stroke({
          color: "#Fff",
        }),
        zIndex: 9
      })
    })
  }
  var ylStyle = style('yl')
  var skStyle = style('sk')
  var hdStyle = style('hd')
  // var ylStyle = new Style({
  //   image: new Circle({
  //     radius: 5,
  //     fill: new Fill({
  //       color: '#05ab57',
  //     }),
  //     stroke: new Stroke({
  //       color: status,
  //       width: 16
  //     }),
  //   }),
  //   text: new Text({
  //     textAlign: "center", // 位置
  //     textBaseline: "top", // 基准线
  //     offsetX: 0,
  //     offsetY: 20,
  //     text: name,
  //     fill: new Fill({
  //       color: "#000",// 文本填充样式(即文字颜色)
  //     }),
  //     stroke: new Stroke({
  //       color: "#Fff",
  //       width: 1,
  //     }),
  //     zIndex: 9
  //   })
  // })

  // // 水库    点
  // var skStyle = new Style({
  //   image: new Icon({
  //     anchor: [0.5, 30],
  //     anchorOrigin: 'bottom-right',
  //     anchorXUnits: 'fraction',
  //     anchorYUnits: 'pixels',
  //     // offsetOrigin: 'bottom-center',
  //     scale: 0.45,
  //     src: sk,

  //   }),
  //   text: new Text({
  //     textAlign: "center", // 位置
  //     textBaseline: "center", // 基准线
  //     offsetX: 0,
  //     offsetY: 30,
  //     // font: "normal 12px 微软雅黑", // 文字样式
  //     text: name,
  //     fill: new Fill({
  //       color: "#333",// 文本填充样式(即文字颜色)
  //     }),
  //     stroke: new Stroke({
  //       color: "#Fff",
  //     }),
  //     zIndex: 9
  //   })
  // })

  // // 河道    点
  // var hdStyle = new Style({
  //   image: new Icon({
  //     anchor: [0.5, 26],
  //     anchorXUnits: 'fraction',
  //     anchorYUnits: 'pixels',
  //     scale: 0.48,
  //     src: hd,
  //   }),
  //   text: new Text({
  //     textAlign: "center", // 位置
  //     textBaseline: "center", // 基准线
  //     offsetX: 0,
  //     offsetY: 30,
  //     // font: "normal 12px 微软雅黑", // 文字样式
  //     text: name,
  //     fill: new Fill({
  //       color: "#333",// 文本填充样式(即文字颜色)
  //     }),
  //     stroke: new Stroke({
  //       color: "#Fff",
  //     }),
  //     zIndex: 9
  //   })
  // })
  return {
    ylStyle,
    skStyle,
    hdStyle
  }