OpenLayers 即时渲染

关注我,带你一起学GIS ^

注:当前使用的是 ol [9.2.4] 版本,天地图使用的key请到天地图官网申请,并替换为自己的key

前言

通常来说,图层必须要添加到地图中才能被渲染。但是,有一种情况比较特殊,那就是即时渲染功能。该模式下可以不用将几何对象添加到地图中就可以实现图形渲染。

1. 基本原理

即时渲染API允许绘制自定义样式几何图形,而无需首先将它们添加到层中。可以使用getVectorContext函数获取渲染事件上下位对象。在此渲染上下文中使用context.drawGeometry()context.setStyle()方法,可以在每个渲染帧上绘制任何几何图形。

2. 创建随机点

在地图上创建一些随机点,模拟几何图形。

// 随机创建10000个几何对象
const geoNum = 10000
const geomes = []
for (let i = 0; i < geoNum; i++) {
    const lng = 85 + 90 * Math.random()
    const lat = -30 + 65 * Math.random()
    geomes.push(new ol.geom.Point([lng, lat]))
}

3. 监听地图事件

在地图事件postrender中获取当前渲染事件上下文,在遍历随机点时,通过ol.easing.upAndDown方法返回其弹性值,其值在0和1之间。然后使用渲染事件上下文自定义样式并绘制几何对象,重新渲染地图。

TDTImgLayer.on("postrender", evt => {
    const vectorContext = ol.render.getVectorContext(evt)
    for (let i = 0; i < geoNum; i++) {
        const flex = ol.easing.upAndDown(Math.pow((geoNum - i) / geoNum, 0.15))
        if (flex < 0.1) {
            continue
        }
        circle.setOpacity(flex)
        circle.setScale(flex)
        vectorContext.setStyle(circleStyle)
        vectorContext.drawGeometry(geomes[i])
    }

    const lng = 85 + 90 * Math.random()
    const lat = -30 + 65 * Math.random()
    geomes.push(new ol.geom.Point([lng, lat]))

    geomes.shift()
    map.render()
})

4. 完整代码

其中libs文件夹下的包需要更换为自己下载的本地包或者引用在线资源。

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenLayers 即时渲染</title>
    <meta charset="utf-8" />

    <link rel="stylesheet" href="../../libs/css/ol9.2.4.css">

    <script src="../../js/config.js"></script>
    <script src="../../libs/js/ol9.2.4.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
            font-size: 14px;
            font-family: '微软雅黑';
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        #map {
            position: absolute;
            top: 50px;
            bottom: 0;
            width: 100%;
            background: #E0ECED;
        }

        #top-content {
            position: absolute;
            width: 100%;
            height: 50px;
            line-height: 50px;
            background: linear-gradient(135deg, #ff00cc, #ffcc00, #00ffcc, #ff0066);
            color: #fff;
            text-align: center;
            font-size: 32px;
        }

        #top-content span {
            font-size: 32px;
        }
    </style>
</head>

<body>
    <div id="top-content">
        <span class="removeAllLayer">OpenLayers 即时渲染</span>
    </div>

    <div id="map"></div>

</body>

</html>

<script>

    //地图投影坐标系
    const projection = ol.proj.get('EPSG:3857');
    //==============================================================================//
    //============================天地图服务参数简单介绍==============================//
    //================================vec:矢量图层==================================//
    //================================img:影像图层==================================//
    //================================cva:注记图层==================================//
    //======================其中:_c表示经纬度投影,_w表示球面墨卡托投影================//
    //==============================================================================//
    const TDTImgLayer = new ol.layer.Tile({
        title: "天地图影像图层",
        source: new ol.source.XYZ({
            url: "http://t0.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=" + TDTTOKEN,
            attibutions: "天地图影像描述",
            crossOrigin: "anoymous",
            wrapX: false
        })
    })
    const TDTImgCvaLayer = new ol.layer.Tile({
        title: "天地图影像注记图层",
        source: new ol.source.XYZ({
            url: "http://t0.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=" + TDTTOKEN,
            attibutions: "天地图注记描述",
            crossOrigin: "anoymous",
            wrapX: false
        })
    })

    const map = new ol.Map({
        target: "map",
        loadTilesWhileInteracting: true,
        view: new ol.View({
            center: [101.485106, 25.008643],
            zoom: 5,
            worldsWrap: false,
            minZoom: 1,
            maxZoom: 20,
            projection: 'EPSG:4326',
        }),
        layers: [TDTImgLayer],
        // 地图默认控件
        controls: ol.control.defaults.defaults({
            zoom: false,
            attribution: true,
            rotate: true
        })
    })

    const circle = new ol.style.Circle({
        radius: 5,
        fill: new ol.style.Fill({
            color: "yellow"
        })
    })
    const circleStyle = new ol.style.Style({
        image: circle
    })

    // 随机创建1000个几何对象
    const geoNum = 10000
    const geomes = []
    for (let i = 0; i < geoNum; i++) {
        const lng = 75 + 90 * Math.random()
        const lat = -30 + 65 * Math.random()
        geomes.push(new ol.geom.Point([lng, lat]))
    }

    TDTImgLayer.on("postrender", evt => {
        const vectorContext = ol.render.getVectorContext(evt)
        for (let i = 0; i < geoNum; i++) {
            const flex = ol.easing.upAndDown(Math.pow((geoNum - i) / geoNum, 0.15))
            if (flex < 0.1) {
                continue
            }
            circle.setOpacity(flex)
            circle.setScale(flex)
            vectorContext.setStyle(circleStyle)
            vectorContext.drawGeometry(geomes[i])
        }

        const lng = 85 + 90 * Math.random()
        const lat = -30 + 65 * Math.random()
        geomes.push(new ol.geom.Point([lng, lat]))

        geomes.shift()
        map.render()
    })
</script>

OpenLayers示例数据下载,请在公众号后台回复:ol数据

全国信息化工程师-GIS 应用水平考试资料,请在公众号后台回复:GIS考试

GIS之路公众号已经接入了智能助手,欢迎大家前来提问。

欢迎访问我的博客网站-长谈GIShttp://shanhaitalk.com

都看到这了,不要忘记点赞、收藏+关注 

本号不定时更新有关 GIS开发  相关内容,欢迎关注 

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部