我在地图上绘制要素(WKT POINTs)。现在我想给这些点一个半径。在我将功能和图层添加到地图之前,我会执行以下操作:
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 ol.layer.Tile”中,我用来在后台显示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,为什么矢量图层上的特征会以这种样式呈现?
我的问题:
这样做的正确方法是什么?
是否有“风格继承”?
答案 0 :(得分:2)
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