本文基于腾讯课堂老胡的课《跟我学Openlayers--基础实例详解》做的学习笔记,使用的openlayers 5.3.x api。
源码 见 1027.html ,对应的 官网示例
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>
<button id="set-source" class="code">layer.setSource(source)</button>
<button id="unset-source" class="code">layer.setSource(null)</button>
<div id="map" class="map"></div>
<script>
var source = new ol.source.OSM();
var layer = new ol.layer.Tile();
var map = new ol.Map({
layers: [layer],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
document.getElementById('set-source').onclick = function () {
layer.setSource(source);
};
document.getElementById('unset-source').onclick = function () {
layer.setSource(null);
};
</script>
</body>
</html>
网友评论