【问题标题】:Rule based Styling in openlayers 3openlayers 3中基于规则的样式
【发布时间】:2015-09-24 15:57:17
【问题描述】:

我正在尝试在 openlayers 3 中进行基于规则的样式设置,这是我的代码:

<!DOCTYPE html>
<html>
<head>
<title>CONFIG</title>


<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://openlayers.org/en/v3.8.2/css/ol.css" type="text/css">
<script src="http://openlayers.org/en/v3.8.2/build/ol.js"></script>

</head>
<body>
<div class="container-fluid">

<div class="row-fluid">
  <div class="span12">
    <div id="map" class="map"></div>
  </div>
</div>
<script>

    var layerStyle   = {
         default: {
        style: new ol.style.Style({
        stroke: new ol.style.Stroke({
        color: 'rgba(0, 0, 0, 1.0)',
        width: 0.5
            }),
        fill: new ol.style.Fill({
        color: 'rgba(0, 255, 0, 0.5)'
            })
          })
        },

        filter: [{
        type: "AND",
        rules: [{
            type: "==",
            property: "CODE",
            value: "16"
        }, {
            type: "==",
            property: "NAME",
            value: "CHIKMAGALUR"
        }],
        symbolizers: {
        style: new ol.style.Style({
        stroke: new ol.style.Stroke({
                color: 'rgba(0, 255, 0, 0.5)',
                width: 0.5
            }),
        fill: new ol.style.Fill({
                color: 'red'
            })
          })
        }
    } ]
    };

    var geojson_layer = new ol.layer.Vector({
    source: new ol.source.Vector({
        url: 'file.geojson',
        format: new ol.format.GeoJSON()
    }),
});
var vectorLayer = new ol.source.Vector({
        features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
    });

        if ('CODE'=='16' && 'NAME'=='CHIKMAGALUR') {
        style: symbolizers;
        }




        var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    }),
   vectorLayer, 
  ],
  target: 'map',
  controls: ol.control.defaults({
    attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
      collapsible: false
    })
  }),
  view: new ol.View({
    center: [0, 0],
    zoom: 5
  })
});

</script>
</body>
</html>

我正在尝试从符号器中获取样式,但出现错误。像 openlayer2 一样,我使用过滤器进行 AND 操作,但我卡住了。有人可以帮我吗?

【问题讨论】:

  • openlayers 标签(对于 OL 2)添加到您的问题中,看看 Openlayers 2 是否可以帮助您找到 Openlayers 3 上的等价物。另外,请阅读 API 文档,如果您尝试使用 API 上不存在的属性,您总是会收到错误消息。

标签: openlayers-3


【解决方案1】:

在 OL3 中,矢量图层有一个 style 属性,可以是 Style 对象、Style 对象数组或返回 Style 对象或 Style 对象数组的函数。您可能想要使用样式函数来完成您正在尝试做的事情。

请参阅以下示例了解如何做到这一点:

http://openlayers.org/en/v3.20.1/examples/earthquake-clusters.html

【讨论】:

  • 谢谢 Bob Holmes,我不能在 ol3 中应用过滤器吗??
  • 不完全是,至少没有那个名字。但是,您可以在样式函数中实现过滤器,为应该显示的所有功能返回一个样式对象,并为应该过滤掉的所有功能返回 null
猜你喜欢
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多