ol.style.Circle 与 ol.layer.Vector 抛出错误

ol.style.Circle with ol.layer.Vector throws error

我在地图上绘制要素(WKT 点)。现在我想给这些点一个半径。在我向地图添加要素和图层之前,我执行以下操作:

var src = new ol.source.Vector();
var layer = new ol.layer.Vector({
    source: src,
    style: new ol.style.Circle({
        radius: 30
    })
});

这会引发以下错误:

AssertionError: Assertion failed: obj geometry must be an ol.style.Style instance
    at goog.asserts.DEFAULT_ERROR_HANDLER (http://localhost:33464/app/lib/openLayers/ol-debug.js:4330:52)
    at goog.asserts.doAssertFailure_ (http://localhost:33464/app/lib/openLayers/ol-debug.js:4365:3)
    at goog.asserts.assertInstanceof (http://localhost:33464/app/lib/openLayers/ol-debug.js:4575:5)
    at ol.style.createStyleFunction (http://localhost:33464/app/lib/openLayers/ol-debug.js:56402:7)
    at ol.layer.Vector.prototype.setStyle (http://localhost:33464/app/lib/openLayers/ol-debug.js:58228:3)
    at ol.layer.Vector (http://localhost:33464/app/lib/openLayers/ol-debug.js:58115:3)

如果我将相同的样式添加到我用来在后台显示 OpenStreetMap (ol.source.OSM) 的 "new ol.layer.Tile",则一切正常:

map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM(),
        style: new ol.style.Circle({
            radius: 30
        })
      })
    ]
});

我不太明白。我想这与 ol.style.Circle 扩展 ol.style.Image 有关(这听起来不像是矢量层的东西——而是栅格层 "Tile")。但是如果我在Tile-Layer上添加样式,为什么vector-Layer渲染上的特征都是这个样式?

我的问题:

  1. 正确的做法是什么?

  2. 有"style-Inheritance"活动吗?

style 不是 ol.layer.Tile 对象的有效选项,因此它被简单地忽略了。请参阅其文档:http://openlayers.org/en/v3.10.1/apidoc/ol.layer.Tile.html

ol.layer.Vector 中定义的样式定义必须是以下之一:

  • 一个ol.style.Style对象
  • ol.style.Style 个对象的数组
  • 样式函数,即ol.style.StyleFunction

因此,为了让您的示例正常工作,您可以定义如下所示的样式:

var src = new ol.source.Vector();
var layer = new ol.layer.Vector({
    source: src,
    style: new ol.style.Style({
        image: new ol.style.Circle({
            radius: 30,
            fill: new ol.style.Fill({color: 'red'})
        })
    })
});

此示例演示自定义样式:http://openlayers.org/en/v3.10.1/examples/custom-interactions.html

另请参阅 API 文档:http://openlayers.org/en/v3.10.1/apidoc/ol.layer.Vector.html