所以我正在使用单个矢量图层放置我的所有内容:
这是我的代码:
var source = new ol.source.Vector({ wrapX: false });
var vector = new ol.layer.Vector({
source: source,
style: // styleFunction or style or [style] don't know...
});
我想根据类型设置功能。我在documentation中找到了这个,但无法弄清楚如何使用它:
... 单独的编辑样式具有以下默认值:
styles[ol.geom.GeometryType.POLYGON] = [
new ol.style.Style({
fill: new ol.style.Fill({
color: [255, 255, 255, 0.5]
})
})
];
styles[ol.geom.GeometryType.LINE_STRING] = [
...
];
styles[ol.geom.GeometryType.POINT] = [
...
];
有什么想法吗?
答案 0 :(得分:3)
查看官方拖放示例 - > ol3 example
它处理所有可能的几何。
因此,声明你的样式对象
var defaultStyle = {
'Point': new ol.style.Style({
image: new ol.style.Circle({
fill: new ol.style.Fill({
color: 'rgba(255,255,0,0.5)'
}),
radius: 5,
stroke: new ol.style.Stroke({
color: '#ff0',
width: 1
})
})
}),
'LineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#f00',
width: 3
})
}),
'Polygon': new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(0,255,255,0.5)'
}),
stroke: new ol.style.Stroke({
color: '#0ff',
width: 1
})
}),
'MultiPoint': new ol.style.Style({
image: new ol.style.Circle({
fill: new ol.style.Fill({
color: 'rgba(255,0,255,0.5)'
}),
radius: 5,
stroke: new ol.style.Stroke({
color: '#f0f',
width: 1
})
})
}),
'MultiLineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#0f0',
width: 3
})
}),
'MultiPolygon': new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(0,0,255,0.5)'
}),
stroke: new ol.style.Stroke({
color: '#00f',
width: 1
})
})
};
然后创建你的风格函数
var styleFunction = function(feature, resolution) {
var featureStyleFunction = feature.getStyleFunction();
if (featureStyleFunction) {
return featureStyleFunction.call(feature, resolution);
} else {
return defaultStyle[feature.getGeometry().getType()];
}
};
最后,将样式函数设置为矢量图层
map.addLayer(new ol.layer.Vector({
source: new ol.source.Vector({}),
style: styleFunction
}));
答案 1 :(得分:2)
获取几何类型,然后根据几何类型
应用样式 style:function(feature, resolution){
var geom_name = feature.getGeometry().getType();
return styles[geom_name];
}
})