openlayer中geojson要素图层设置style样式

目录

填充、描边

动态设置文字标注:

随机设置颜色:

 添加图标

获取feature后,可以通过Feature.setStyle()函数给feature动态设置样式:

          feature.setStyle(
            new Style({
              //填充色
              fill: new Fill({
                color: "rgba(255, 255, 255, 0.2)",
              }),
              //边线颜色
              stroke: new Stroke({
                color: "#FF0000",
                width: 5,
              }),
              text: new Text({
                // 位置
                textAlign: "center",
                // 基准线
                textBaseline: "middle",
                // 文字样式
                font: "bold 18px 微软雅黑",
                // 文本内容
                text: `${feature.get("name")}(${feature.get("area")}亩)`,
                // 文字颜色
                fill: new Fill({ color: "#00FFFF" }),
                // 文字背景
                stroke: new Stroke({ color: "#353535", width: 1 }),
              }),
               //点样式,继承image
              image: new Circle({
                radius: 7,
                fill: new Fill({
                  color: "#ffcc33",
                }),
              }),
        // image: new Icon({
        //   anchor: [0.5, -0.3],
        //   src: "https://openlayers.org/en/v4.6.5/examples/data/icon.png",
        //   scale: 1, //设置大小
        // }),

            })
          );

填充、描边

import "ol/ol.css";
import { Map, View } from "ol";
import { TileWMS, OSM, Vector as VectorSource } from "ol/source";
import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer";
import GeoJSON from "ol/format/GeoJSON";
import { Fill, Stroke, Style, Text, Icon, Circle } from "ol/style";


new VectorLayer({
          source: new VectorSource({
            url: "xxx",
            format: new GeoJSON()
          }),
          style: new Style({
            fill: new Fill({
              //矢量图层填充颜色,以及透明度
              color: "rgba(33,194,219,0.5)"
            }),
            stroke: new Stroke({
              //边界样式
              color: "rgba(100, 90, 209, 0.6)",
              width: 3
            }),
            text: new Text({
              //文本样式
              font: "12px Calibri,sans-serif",
              fill: new Fill({
                color: "#000"
              }),
              stroke: new Stroke({
                color: "#fff",
                width: 3
              })
            })
          })
        })

动态设置文字标注:

随机设置颜色:

let colors = ["orange","red","blue","yellow"];
let color = colors[Math.floor(Math.random()*colors.length)];

 添加图标

          new VectorLayer({
            source: new VectorSource({
              features: new GeoJSON().readFeatures(this.geojsonData_sensor),
            }),
            style: function (feature) {
              return new Style({
                fill: new Fill({
                  //矢量图层填充颜色,以及透明度
                  color: "#4e98f444",
                }),
                stroke: new Stroke({
                  //边界样式
                  color: "rgba(71, 137, 227, 1)",

                  width: 3,
                }),
                text: new Text({
                  // 位置
                  textAlign: "center",
                  // 基准线
                  textBaseline: "middle",
                  // 文字样式
                  font: "bold 18px 微软雅黑",
                  // 文本内容
                  text: `${feature.get("title")}`,
                  // 文字颜色
                  fill: new Fill({
                    color: "black",
                  }),
                  // 文字背景
                  stroke: new Stroke({ color: "yellow", width: 10 }),
                }),
                image: new Icon({
                  anchor: [0.5, -0.3],
                  src: "https://openlayers.org/en/v4.6.5/examples/data/icon.png",
                  scale: 1, //设置大小
                }),
              });
            },
            zIndex: 999,
          }),

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue.js是一个流行的JavaScript框架,用于构建用户界面。OpenLayers是一个开源的JavaScript库,用于在Web上显示交互式地图。结合Vue.js和OpenLayers,可以实现读取和展示GeoJSON数据文件的功能。 首先,你需要在Vue.js项目安装OpenLayers库。可以通过npm或者yarn来安装OpenLayers依赖: ``` npm install ol ``` 或者 ``` yarn add ol ``` 安装完成后,在Vue组件引入OpenLayers库: ```javascript import 'ol/ol.css'; import { Map, View } from 'ol'; import GeoJSON from 'ol/format/GeoJSON'; import VectorLayer from 'ol/layer/Vector'; import VectorSource from 'ol/source/Vector'; export default { mounted() { this.loadGeoJSON(); }, methods: { loadGeoJSON() { // 读取GeoJSON文件 fetch('path/to/your/geojson/file.geojson') .then(response => response.json()) .then(data => { // 创建矢量源 const vectorSource = new VectorSource({ features: new GeoJSON().readFeatures(data), }); // 创建矢量图层 const vectorLayer = new VectorLayer({ source: vectorSource, }); // 创建地图 const map = new Map({ target: 'map', // HTML元素的id,用于显示地图 layers: [vectorLayer], view: new View({ center: [0, 0], // 地图心点的坐标 zoom: 10, // 地图缩放级别 }), }); }); }, }, }; ``` 上述代码,`loadGeoJSON`方法用于读取GeoJSON文件并创建地图。首先使用`fetch`函数获取GeoJSON文件的数据,然后使用`GeoJSON`格式化器将数据转换为矢量要素。接着,创建矢量源和矢量图层,并将矢量图层添加到地图。最后,创建地图对象并显示在指定的HTML元素(通过`target`属性指定)。 请注意,上述代码的`path/to/your/geojson/file.geojson`需要替换为实际的GeoJSON文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值