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,
          }),

在线预览:

WebGIS之家WebGIS之家,openlayers示例源码,cesium示例源码,openlayers在线调试预览,cesium在线调试预览,webgis,数字地球,数字孪生icon-default.png?t=N7T8https://www.webgishome.com/preview?id=55&example_name=style2&title=%E6%A0%B7%E5%BC%8F2

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
OpenLayers是一个开源的JavaScript库,用于在Web浏览器显示交互式地图。它提供了丰富的功能和工具,包括导出GeoJSON数据。 要导出GeoJSON数据,你可以使用OpenLayers的`format`模块的`GeoJSON`类。首先,你需要将地图上的要素(features)转换为GeoJSON格式,然后将其保存到文件或进行其他处理。 以下是一个简单的示例代码,演示如何使用OpenLayers导出GeoJSON数据: ```javascript // 创建一个地图实例 var map = new ol.Map({ // 设置地图容器的ID target: 'map', // 设置地图图层 layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], // 设置地图视图 view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 2 }) }); // 创建一个要素集合 var features = new ol.Collection(); // 创建一个矢量图层 var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: features }) }); // 将矢量图层添加到地图 map.addLayer(vectorLayer); // 添加一些要素要素集合(这里只是示例,你可以根据自己的需求添加要素) features.push(new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([0, 0])), name: 'Point 1' })); features.push(new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([10, 10])), name: 'Point 2' })); // 创建一个GeoJSON格式化器 var geojsonFormat = new ol.format.GeoJSON(); // 将要素集合转换为GeoJSON字符串 var geojsonStr = geojsonFormat.writeFeatures(features.getArray()); // 打印输出GeoJSON字符串 console.log(geojsonStr); ``` 在上面的示例,我们创建了一个地图实例,并添加了一个矢量图层。然后,我们创建了一些要素,并将它们添加到要素集合。最后,我们使用`ol.format.GeoJSON`将要素集合转换为GeoJSON字符串,并将其打印输出。 请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。另外,你可以使用其他方法来保存或处理导出的GeoJSON数据。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值