Openlayer中给feature设置不同的样式

本文介绍如何使用GeoJSON数据创建地图,并展示了两种方式:一是通过`newVectorLayer`设置静态样式,二是动态通过`feature.setStyle()`调整。详细讲解了如何配置多边形区域和添加名称标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:

 

在线预览:

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

方式一:

geojson数据: 

geojsonData: {
        type: "FeatureCollection",
        features: [
          {
            type: "Feature",
            properties: {         
              name: "地块1",
              style: {
                fill: {
                  color: "rgba(255,255,0,0.2)",
                },
                stroke: {
                  color: "rgba(255,255,0,0.6)",
                },
              },
            },
            geometry: {
              type: "MultiPolygon",
              coordinates: [
                [
                  [
                    [104.29969588135856, 30.526679760778393],
                    [104.29992193015403, 30.526209817229756],
                    [104.29919619454733, 30.526001614391873],
                    [104.29897609440422, 30.52647750659287],
                    [104.29969588135856, 30.526679760778393],
                  ],
                ],
              ],
            },
          }
        ],
      },

使用:

      this.map = new Map({
        target: "map",
        layers: [
          new VectorLayer({
            name: "dk",
            source: new VectorSource({
              features: new GeoJSON().readFeatures(this.geojsonData),
            }),
            style: function (feature) {
              return new Style({
                fill: new Fill({
                  color: feature.get("style").fill.color,
                }),
                stroke: new Stroke({
                  color: feature.get("style").stroke.color,
                  width: 3,
                }),
                text: new Text({
                  textAlign: "center",
                  textBaseline: "middle",
                  font: "bold 18px 微软雅黑",
                  text: `${feature.get("name")}`,
                  fill: new Fill({ color: "#00FFFF" }),
                  stroke: new Stroke({ color: "#353535", width: 1 }),
                }),
              });
            },
            zIndex: 999,
          }),
        ],
        view: new View({
          projection: "EPSG:4326",
          center: [104.29806, 30.5263],
          zoom: 18,
        }),
      });

方式二:

通过feature.setStyle()设置样式,通过feature.getStyle()获取样式

feature.setStyle(() => {
    return new Style({
     stroke: new Stroke({
         color: "#07B9EE",
         lineDash: [7, 10],
         width: 2
         })
    })
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值