Openlayer中给feature设置不同的样式

效果图:

 

方式一:

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

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenLayers,source是指地图数据的来源,可以是各种不同的数据格式。其,通过layer获取数据源可以通过`layer.getSource()`来实现。可以通过`source.addFeature(feature)`来给source添加单个feature,也可以通过`source.addFeatures([feature1,feature2,xxx])`来给source添加多个feature。通过`source.getProperties()`可以获取source的所有属性。判断source的类型可以使用`if (source instanceof VectorSource){}`。GeoJSON是一种常用的数据格式,用于编码各种地理数据结构。可以通过url获取GeoJSON格式的数据。需要记住source是layer必须的选项,定义着地图数据的来源,而且source支持多种数据格式。可以使用函数创建要素数据,也可以使用GeoJSON格式数据。在OpenLayers,可以使用`ol.source.Vector`来创建矢量图层的source,通过`features`参数传入使用`ol.format.GeoJSON().readFeatures(geojsonObject)`获取的要素数据。然后可以创建一个`ol.layer.Vector`图层,将该source设置为图层的source,并定义样式。最后,可以通过`map.addLayer(vectorLayer)`将该图层添加到地图。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Openlayer的source数据源](https://blog.csdn.net/qq_40323256/article/details/120063007)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [OpenLayers - Source简介 (四)](https://blog.csdn.net/yy729851376/article/details/123031013)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值