天下之事,必先处之难,而后易之。
君临
知我者谓我心忧,不知我者谓我何求

原文: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;
    }

 

posted on 2019-02-10 14:51  boonya  阅读(2272)  评论(0编辑  收藏  举报

我有佳人隔窗而居,今有伊人明月之畔。
轻歌柔情冰壶之浣,涓涓清流梦入云端。
美人如娇温雅悠婉,目遇赏阅适而自欣。
百草层叠疏而有致,此情此思怀彼佳人。
念所思之唯心叩之,踽踽彳亍寤寐思之。
行云如风逝而复归,佳人一去莫知可回?
深闺冷瘦独自徘徊,处处明灯影还如只。
推窗见月疑是归人,阑珊灯火托手思忖。
庐居闲客而好品茗,斟茶徐徐漫漫生烟。

我有佳人在水之畔,瓮载渔舟浣纱归还。
明月相照月色还低,浅近芦苇深深如钿。
庐山秋月如美人衣,画堂春阁香气靡靡。
秋意幽笃残粉摇曳,轻轻如诉画中蝴蝶。
泾水潺潺取尔浇园,暮色黄昏如沐佳人。
青丝撩弄长裙翩翩,彩蝶飞舞执子手腕。
香带丝缕缓缓在肩,柔美体肤寸寸爱怜。
如水之殇美玉成欢,我有佳人清新如兰。
伊人在水我在一边,远远相望不可亵玩。