Openlayers - 不同几何类型的多种样式
Openlayers - Multiple styles for different Geometry Types
所以我使用了一个 单一 向量层,我把我所有的:
- 积分
- 折线
- 多边形
这是我的代码:
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 中找到了它,但不知道如何使用它:
...
A separate editing style has the following defaults:
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] = [
...
];
有什么想法吗?
获取几何类型,然后根据几何类型应用样式
style:function(feature, resolution){
var geom_name = feature.getGeometry().getType();
return styles[geom_name];
}
})
查找演示 link
https://plnkr.co/edit/kOzfXyv36UxXke8bALqU?p=preview
查看官方drag/drop示例-->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
}));
所以我使用了一个 单一 向量层,我把我所有的:
- 积分
- 折线
- 多边形
这是我的代码:
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 中找到了它,但不知道如何使用它:
... A separate editing style has the following defaults:
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] = [
...
];
有什么想法吗?
获取几何类型,然后根据几何类型应用样式
style:function(feature, resolution){
var geom_name = feature.getGeometry().getType();
return styles[geom_name];
}
})
查找演示 link https://plnkr.co/edit/kOzfXyv36UxXke8bALqU?p=preview
查看官方drag/drop示例-->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
}));