OpenLayer4——图层叠加

叠加一张png

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <link href="ol/ol.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="ol/ol.js" charset="utf-8"></script>
</head>
<body>
<div id="map" style="width: 100%;height: 100%"></div>
<script>
    var map = new ol.Map({
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM(),
            })
        ],
        target: 'map',
        view: new ol.View({
            projection: ol.proj.get('EPSG:4326').getCode(),
            center: [110.938071, 36.306347],
            zoom: 3
        })
    });

    //地图上叠加新的图层
    var image = new ol.layer.Image({
        imageExtent: [116.5, 54.5, 72.4, 35.5],
        source: new ol.source.ImageStatic({
            url: './img/cloud.png',
            crossOrigin: '',
            imageExtent: [116.5, 54.5, 72.4, 35.5]
        })
    });

    map.addLayer(image);
</script>
</body>
</html>
复制代码

效果如下:

 

 图片加载侦听

 

//source 指ImageStatic 对象

source.on('imageloadend', function() {
    //todo sth.
});
source.on('imageloaderror', function() {
    //todo sth.
});

切换图片资源

更新一个Image和更新feature的数据一样,不要去删除原本旧的Layer层,在原有的layer对象上set新的资源(或者数据)。
删除一整个图层的时候,会出现画面闪烁的情况,要尽量避免频繁删减图层。

复制代码
    let image = null;
    if (image) {
        //如果存在图层,则替换图片资源
        var source = new ol.source.ImageStatic({
            url: url,
            imageExtent: [136.5, 54.5, 72.4, 15.5]
        });
        image.setSource(source);
    } else {
        //如果不存在,则新建一个Image对象,并且添加图片资源
        image = new ol.layer.Image({
            source: new ol.source.ImageStatic({
                url: url,
                imageExtent: [136.5, 54.5, 72.4, 15.5]
            }),
            opacity: 0.5,
        });
        map.addLayer(image);
    }
复制代码

 

posted on   疯狂的妞妞  阅读(308)  评论(0编辑  收藏  举报

(评论功能已被禁用)
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2020-03-25 SpringCloud(三)服务消费
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示