一、图层样式配置规则
- 规则配置使用到 OpenLayers 中的比较过滤器 OpenLayers.Filter.Comparison
- 常见的属性有
- type:比较的类型
- property:要比较的上下文属性的名称
- value:用于二进制比较的{Number}或{String}比较值
let getstyle = () => {
var style = new OpenLayers.Style(
{
pointRadius: 6,
fillOpacity: 1,
strokeWidth: 1,
strokeOpacity: 1,
graphicZIndex: 1
},
{
rules: [
new OpenLayers.Rule({
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "color_flag",
value: 1
}),
symbolizer: {
fillOpacity: 1,
fillColor: "#FFFF00",
strokeWidth:1,
strokeOpacity: 1,
strokeColor: "#FFFF00"
}
}),
new OpenLayers.Rule({
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "color_flag",
value: 2
}),
symbolizer: {
fillOpacity: 1,
fillColor: "#CDCD00",
strokeWidth:1,
strokeOpacity: 1,
strokeColor: "#CDCD00"
}
}),
new OpenLayers.Rule({
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "color_flag",
value: 3
}),
symbolizer: {
fillOpacity: 1,
fillColor: "#ED1C24",
strokeWidth:1,
strokeOpacity: 1,
strokeColor: "#ED1C24"
}
}),
new OpenLayers.Rule({
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "color_flag",
value: 4
}),
symbolizer: {
fillOpacity: 1,
fillColor: "#CD3232",
strokeWidth:1,
strokeOpacity: 1,
strokeColor: "#CD3232"
}
}),
new OpenLayers.Rule({
elseFilter: true,
symbolizer: {
fillOpacity: 1,
fillColor: "#FFFF00",
strokeWidth:1,
strokeOpacity: 1,
strokeColor: "#FFFF00"
}
})
]
}
);
return style
};
二、使用
// 1.创建一个图层,样式使用的就是上面定义的样式
let layers = new OpenLayers.Layer.Vector(
"基础图层",
{
styleMap:new OpenLayers.StyleMap({
"default": getstyle()
})
});
map.addLayer(layers);
// 2.面信息
let polygon = "POLYGON((12620066.678926 2647759.0245114,12620066.678926 2647729.0245114,12620096.678926 2647729.0245114,12620096.678926 2647759.0245114,12620066.678926 2647759.0245114))";
// 3.读取feature
let feature = new OpenLayers.Format.WKT().read(polygon);
// 4.扩展一个属性color_flag
$.extend(feature.attributes, { color_flag : 1 });
// 4.添加feature到图层上
map.getLayersByName('基础图层')[0].addFeatures(feature);
三、扩展
- type:比较的类型
- OpenLayers.Filter.Comparison.EQUAL_TO =“ ==”;
- OpenLayers.Filter.Comparison.NOT_EQUAL_TO =“!=”;
- OpenLayers.Filter.Comparison.LESS_THAN =“ <”;
- OpenLayers.Filter.Comparison.GREATER_THAN =“>”;
- OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO =“ <=”;
- OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO =“> =”;
- OpenLayers.Filter.Comparison.BETWEEN =“ ..”;
- OpenLayers.Filter.Comparison.LIKE =“〜”;
- OpenLayers.Filter.Comparison.IS_NULL =“ NULL”;
- OpenLayers.Format.WKT
- read(polygon) =>返回一个Feature对象
- write(feature) =>返回一个字符串的面的信息