OpenLayer4——图层叠加
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<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); }