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



我在地图上绘制特征(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)

如果我给new oil .layer添加相同的样式。"我用来在后台显示OpenStreetMap (ol.source.OSM),一切都很完美:

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,为什么矢量层上的特征呈现为这种样式?

我的问题:

  1. 正确的做法是什么?

  2. 是否存在"style-Inheritance"?

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

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

  • a 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

最新更新