meface/docs/article/gis/openlayers/55olstyle.md

246 lines
8.3 KiB
Markdown
Raw Normal View History

2023-11-17 10:54:23 +08:00
---
title: Style
date: 2020-10-10
author: ac
tags:
- OpenLayers
- Style
categories:
- GIS
---
> Style作为矢量要素渲染样式的容器包含渲染要素边界border的`stroke`轮廓样式、内部填充的`fill`样式、用于标记的`text`文本样式等。
## 1. 默认样式
当要素`Feature`没有设置`style`样式时,会使用一个[默认的样式](https://openlayers.org/en/latest/apidoc/module-ol_style_Style-Style.html),对几何对象进行渲染。
```javascript
import {Fill, Stroke, Circle, Style} from 'ol/style';
var fill = new Fill({
color: 'rgba(255,255,255,0.4)'
});
var stroke = new Stroke({
color: '#3399CC',
width: 1.25
});
var styles = [
new Style({
image: new Circle({
fill: fill,
stroke: stroke,
radius: 5
}),
fill: fill,
stroke: stroke
})
];
```
跟样式一起使用的还有`ol.color`,用于设置样式的颜色,可以通过数组`[r,g,b,a]`的形式创建`color`实例。
## 2. 构造函数
在创建`style`实例的时候可以在构造参数opt_options中添加`stroke`、`fill`、`text`、`image`样式等或是配置渲染函数`renderer`。但当其renderer属性被指定时其fill、image、stroke属性将无效。
opt_options
| name | type | Description |
| -------- | ------------------------------------------------------------ | ---------------- |
| fill | [ol/style/Fill~Fill](https://openlayers.org/en/latest/apidoc/module-ol_style_Fill-Fill.html) | 填充样式。 |
| image | [ol/style/Image~ImageStyle](https://openlayers.org/en/latest/apidoc/module-ol_style_Image-ImageStyle.html) | 图像样式 |
| renderer | [ol/style/Style~RenderFunction](https://openlayers.org/en/latest/apidoc/module-ol_style_Style.html#~RenderFunction) | 渲染函数 |
| stroke | [ol/style/Stroke~Stroke](https://openlayers.org/en/latest/apidoc/module-ol_style_Stroke-Stroke.html) | 轮廓样式。 |
| text | [ol/style/Text~Text](https://openlayers.org/en/latest/apidoc/module-ol_style_Text-Text.html) | 文本样式 |
| geometry | [ol/geom/Geometry~Geometry](https://openlayers.org/en/latest/apidoc/module-ol_geom_Geometry-Geometry.html)\|<br>[ol/style/Style~GeometryFunction](https://openlayers.org/en/latest/apidoc/module-ol_style_Style.html#~GeometryFunction) | 要渲染的几何对象 |
| zIndex | number | Z方向上的索引 |
注意:
- 对于点、线要素如果style中只设置了fill填充样式是显示不出来的
- 对于点要素如果style中只设置了stroke轮廓样式是显现不出来的。
- 对于点对象使用text文本样式如果placement设置为'point'则text文本正常显示但为line则会报错
- image图片样式作为抽象类接受子类实例`ol/style/Icon``ol/style/Circle``ol/style/RegularShape`)
- Icon图标样式和Circle圆形样式只作用于点要素上线、面要素是没有效果的。
## 3. 示例
### 3.1 Style
> 所有Feature要素的样式都得是Style的实例不能是单独的轮廓、填充、图像等。
```javascript
/**
* 1、ol/style/Style :作为矢量要素渲染器的容器。
* 所有Feature要素的样式都得是Style的实例不能是单独的轮廓、填充、图像等。
* 构造器 options 对象属性:
* geometry要渲染的几何对象
* fill填充样式
* image图片样式
* stroke形状的轮廓样式
* text文本样式
* renderer渲染器
*/
```
### 3.2 Stroke
```javascript
/**
* 2.'ol/style/Stroke :轮廓样式
* 构造器 options 对象属性:
* color轮廓颜色
* width轮廓厚度
* lineCap轮廓端点的样式butt, round, or square.
* lineJoin节点连接的样式butt, round, or square.
* lineDash用于创建虚线
* lineDashOffset
* miterLimit
* 注意对于点要素如果style中只设置了stroke轮廓样式是显现不出来的。
*/
var strokeStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color:[0,225,0,0.8],
width:1.25,
lineDash:[5]
})
})
```
![image-20201118145112894](./images/image-20201118145112894.png)
### 3.3 Fill
```javascript
/**
* 3、'ol/style/Fill':填充样式
* 构造器 options 对象属性:
* color轮廓颜色
* 注意对于点、线要素如果style中只设置了fill填充样式是显示不出来的
*/
var fillStyle = new ol.style.Style({
fill: new ol.style.Fill({
color:[255,0,0,0.6]
})
})
```
![image-20201118150518088](./images/image-20201118150518088.png)
### 3.4 Text
```javascript
/**
* 4、'ol/style/Text':文本样式
* 构造器 options 对象属性:
* font指定文本字体样式specify a custom font weight, size, and family.如:'bold 48px serif'
* maxAngle最大旋转角
* offsetXoffsetY水平方向和垂直方向的偏移量
* overflow当几何对象是面或文本位置为'line'时,是否允许文本内容溢出
* placement文本位置'point'|'line'
* text文本内容
* textAlign文本对齐方式
* rotateWithView是否随View旋转
* fill填充样式字体颜色
* stroke轮廓样式
* backgroundFill文本背景填充样式当placement为point有效
* padding文本内边距[top, right, bottom, left].
*
* 注意对于点对象使用text文本样式如果placement设置为'point'则text文本正常显示但为line则会报错
*/
var textStyle = new ol.style.Style({
text: new ol.style.Text({
text:"文本注记",
font:'bold 16px serif',
padding:[5,15,15,15],
placement:"line",
stroke:new ol.style.Stroke({
color:"#939393",
width:1
}),
overflow:true,
fill:new ol.style.Fill({color:"#FF6B75"}),
backgroundFill:new ol.style.Fill({color:"#FF6B75"})
}),
});
```
![image-20201118160559989](./images/image-20201118160559989.png)
<div style='text-align:center'>默认样式</div>
![image-20201118161555404](./images/image-20201118161555404.png)
<div style='text-align:center'>placement:"point"</div>
![image-20201118162553312](./images/image-20201118162553312.png)
<div style='text-align:center'>placement:"line"</div>
### 3.5 Image
```javascript
/**
* 5、'ol/style/Image':图片样式,作为抽象类接受子类实例(多态),
* 其子类有'ol/style/Icon''ol/style/Circle''ol/style/RegularShape'
* 构造器 options 对象常用属性:
* opacity不透明度
* rotateWithView是否随View旋转
* rotation旋转角度Math.PI/4
* scale
* displacement
*
* 6、'ol/style/Icon' :图标样式
* 构造器 options 对象常用属性:
* img HTMLImageElement对象
* imgSize :图片大小[width, height],只有当img设置而src没有设置时才需要并且在Internet
* Explorer 11中用于SVG图像。提供的imgSize需要匹配图像的实际大小。
* src 图片的uri地址
* size: Icon的大小单位px可以配合offset设置图标的显示区域
*
*
* 7、ol/style/Circle :圆形样式
* 构造器 options 对象常用属性:
* radius半径
* fill填充样式
* stroke形状的轮廓样式
* displacement默认[0,0]
*
* 注意Icon图标样式和Circle圆形样式只作用于点要素上线、面要素是没有效果的
*/
var IconStyle = new ol.style.Style({
image:new ol.style.Icon({
// anchor:[0,0],
// anchorOrigin:'top-right',
// anchorXUnits:'fraction',
// anchorYUnits:'pixels',
// offsetOrigin:"top-right",
src:"./img/26.jpg",
opacity:0.8,
scale:0.25,
// size:[32,32],
// offset:[30,20]
})
})
var CircleStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
stroke: new ol.style.Stroke({
color:[0,255,0,0.5],
width:2
})
})
})
```
![image-20201118174127486](./images/image-20201118174127486.png)