本文基于腾讯课堂老胡的课《跟我学Openlayers--基础实例详解》做的学习笔记,使用的openlayers 5.3.x api。
源码 见 1026.html ,对应的 官网示例 https://openlayers.org/en/latest/apidoc/module-ol_render_Event-RenderEvent.html#context
image.png image.png<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="../include/ol.css" type="text/css">
<script src="../include/ol.js"></script>
</head>
<body>
<input id="swipe" type="range" style="width: 100%">
<div id="map" class="map"></div>
<script>
var osm = new ol.layer.Tile({
source: new ol.source.OSM()
});
var bing = new ol.layer.Tile({
source:
//new ol.source.BingMaps({
// key: "As1HiMj1PvLPlqc_gtM7AqZfBL8ZL3VrjaS3zIb22Uvb9WKhuJObROC-qUpa81U5",
// imagerySet: "Aerial"
// })
new ol.source.TileJSON({
url: 'https://api.tiles.mapbox.com/v4/mapbox.geography-class.json?secure&access_token=pk.eyJ1IjoiYWhvY2V2YXIiLCJhIjoiY2pzbmg0Nmk5MGF5NzQzbzRnbDNoeHJrbiJ9.7_-_gL8ur7ZtEiNwRfCy7Q',
crossOrigin: ''
})
});
var map = new ol.Map({
layers: [osm, bing],
target: "map",
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
var swipe = document.getElementById("swipe");
bing.on("precompose", function (event) {
var ctx = event.context;
var width = ctx.canvas.width * (swipe.value / 100);
ctx.save();
ctx.beginPath();
ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height);
ctx.clip();
});
bing.on("postcompose", function (event) {
var ctx = event.context;
ctx.restore();
});
swipe.addEventListener(
"input",
function () {
map.render();
},
false
);
</script>
</body>
</html>
网友评论