原文:https://blog.csdn.net/gisshixisheng/article/details/80149087

概述

非常细化Openlayers4中的StyleFunction,因为它可以让我非常方便的实现各种效果,本文带你一起一探究竟。
StyleFunction

StyleFunction是一个样式函数,参数包括:feature和resolution,如下图。

不过,一般来说,resolution我用的很少,我一般会用zoom替换掉resolution这个参数;StyleFunction的返回值包括两种:样式或样式数组,如上图API。

样例

随着缩放点大小变化

function styleFunction(feature) {
var _zoom = map.getView().getZoom(),
_radius = _zoom*2; _radius = _radius<2?2:_radius;
_radius = _radius>15?15:_radius; return new ol.style.Style({
image: new ol.style.Circle({
radius: _radius,
fill: new ol.style.Fill({
color: '#ff0000'
}),
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 2
})
})
})
}

2、级别>4的时候出现标注

 function styleFunction(feature) {
var _zoom = map.getView().getZoom(),
_radius = _zoom*2; _radius = _radius<2?2:_radius;
_radius = _radius>15?15:_radius; var _attr = feature.get("attribute");
var _count = _zoom<5?"":_attr.count; return new ol.style.Style({
image: new ol.style.Circle({
radius: _radius,
fill: new ol.style.Fill({
color: '#ff0000'
}),
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 2
})
}),
text: new ol.style.Text({
text: _count.toString(),
font:"bold 12px Times New Roman",
fill: new ol.style.Fill({
color: '#fff'
})
})
})
}

3、样式组合
样式组合

 function styleFunction(feature) {
var styles = []; styles.push(
new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({
color: '#ff0000'
}),
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 2
})
})
})
); styles.push(
new ol.style.Style({
geometry: feature.getGeometry(),
image: new ol.style.RegularShape({
radius1: 10,
radius2: 5,
points: 8,
fill: new ol.style.Fill({
color: '#fff'
})
})
})
); return styles;
}

4、展示线的节点
展示节点

  function styleFunction(feature) {
var styles = []; styles.push(
new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 4
})
})
); var _coords = feature.get("geometry").getCoordinates();
for(var i=0;i<_coords.length;i++){
styles.push(
new ol.style.Style({
geometry: new ol.geom.Point(_coords[i]),
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({
color: '#ffff'
}),
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 2
})
})
})
);
} return styles;
}

5、带箭头的线
带箭头的线

 function styleFunction(feature) {
var styles = []; styles.push(
new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 4
})
})
); var geometry = feature.get("geometry");
geometry.forEachSegment(function(start, end) {
var dx = end[0] - start[0];
var dy = end[1] - start[1];
var rotation = Math.atan2(dy, dx);
// arrows
styles.push(new ol.style.Style({
geometry: new ol.geom.Point(end),
image: new ol.style.Icon({
src: '../data/arrow.png',
anchor: [0.75, 0.5],
rotateWithView: false,
rotation: -rotation
})
}));
}); return styles;
}

巧用Openlayers4的Style的更多相关文章

  1. 巧用style的另类写法

    看到style,不少人可能会说这个我知道,就是控件写属性的话可以通过style来实现代码的复用,单独把这些属性及其参数写成style就可以便捷的调用. <?xml version="1 ...

  2. 前端工程师技能之photoshop巧用系列第三篇——切图篇

    × 目录 [1]切图信息 [2]切图步骤 [3]实战 前面的话 前端工程师除了使用photoshop进行测量之外,更重要的是要使用该软件进行切图.本文是photoshop巧用系列的第三篇——切图篇 切 ...

  3. 前端工程师技能之photoshop巧用系列第二篇——测量篇

    × 目录 [1]测量信息 [2]实战 [3]注意事项 前面的话 前端工程师使用photoshop进行的大量工作实际上是测量.本文是photoshop巧用系列第二篇——测量篇 测量信息 在网页制作中需要 ...

  4. 前端工程师技能之photoshop巧用系列第五篇——雪碧图

    × 目录 [1]定义 [2]应用场景 [3]合并[4]实现[5]维护 前面的话 前面已经介绍过,描述性图片最终要合并为雪碧图.本文是photoshop巧用系列第五篇——雪碧图 定义 css雪碧图(sp ...

  5. 每日学习心得:Linq解决DataTable按照某一列的值排序问题/DataTable 导出CSV文件/巧用text-overflow解决数据绑定列数据展示过长问题

    2013-8-5 1 Linq解决DataTable按照某一列的值排序 在之前的总结中提到过对拼接而成的复合的DataTable按照某一列值的大小排序,那个主要的思想是在新建表结构时将要排序的那一列的 ...

  6. 网站开发进阶(四十二)巧用clear:both

    网站开发进阶(四十二)巧用clear:both 前言 我们在制作网页中用div+css或者称xhtml+css都会遇到一些很诡异的情况,明明布局正确,但是整个画面却混乱起来了,有时候在IE6下看的很正 ...

  7. openlayers4 入门开发系列之小区信号扇形图篇

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  8. openlayers4 入门开发系列之台风轨迹篇

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  9. openlayers4 入门开发系列之船讯篇

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

随机推荐

  1. Hdu-2008

    杭电OJ-2008 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2008 #include<stdio.h> int main() { i ...

  2. 问题 C: Frosh Week(2018组队训练赛第十五场)(签到)

    问题 C: Frosh Week 时间限制: 4 Sec  内存限制: 128 MB提交: 145  解决: 63[提交][状态][讨论版][命题人:admin] 题目描述 Professor Zac ...

  3. spring mvc注解版01

    spring mvc是基于servlet实现的在spring mvc xml版中已经说过了,注解版相较于xml版更加简洁灵活. web项目的jar包: commons-logging-1.1.3.ja ...

  4. POJ.2175.Evacuation Plan(消圈)

    POJ \(Description\) \(n\)个建筑物,每个建筑物里有\(a_i\)个人:\(m\)个避难所,每个避难所可以容纳\(b_i\)个人. 给出每个建筑物及避难所的坐标,任意两点间的距离 ...

  5. 洛谷P1809 过河问题_NOI导刊2011提高(01)

    To 洛谷.1809 过河问题 题目描述 有一个大晴天,Oliver与同学们一共N人出游,他们走到一条河的东岸边,想要过河到西岸.而东岸边有一条小船. 船太小了,一次只能乘坐两人.每个人都有一个渡河时 ...

  6. Django复习2

    一.创建django程序 终端命令:django-admin startproject sitename IDE创建Django程序时,本质上都是自动执行上述命令 其他常用命令: python man ...

  7. [SDOI2017]树点涂色

    Description: Bob有一棵\(n\)个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不 ...

  8. Exception引起的性能问题

    先show一下两段代码,两段代码都能比较好的实现业务逻辑,但是在高并发下,如果传入的参数为空,那么两段代码的性能表现完全不一样. private static string Get(string fi ...

  9. layer.load的使用

    在ajax请求和回调返回的过程中,我们一般都要用到layer.load这个方法 例如: //loading层 var index = layer.load(1, { shade: [0.1,'#fff ...

  10. 读取html文件,让其中的内容和notepad打开这个html的样子一样。

    然后我写了个python代码,让其读取这个html文件后,内容和这个一样: htmlf=open('13144815898.html','r',encoding="utf-8") ...