<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css">
<style>
.map {
height: 400px;
width: 100%;
}
.share {
position: absolute;
top: 10px;
right: 10px;
border: 1px;
border-color: #333;
background-color: #339999;
color: #fff;
box-shadow: 0px 0px 2px #666;
cursor: pointer;
padding: 0 4px 0 4px;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>OpenLayers example</title>
</head>
<body>
<h2>My Map</h2>
<div id="map" class="map"></div>
<script type="text/javascript">
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
view: new ol.View({
center: ol.proj.transform(
[106.70, 26.62], 'EPSG:4326', 'EPSG:3857'),
zoom: 10
})
});
// 在viewport节点下添加一个分享按钮
var viewport = map.getViewport();
$(viewport).append('<div id="share" class="share">分享地图</div>');
// 监听按钮点击事件,执行相关操作
document.getElementById('share').onclick = function() {
alert('分享当前地图给朋友');
}
</script>
</body>
</html>
##自适配区
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css">
<style>
.map {
height: 800px;
width: 100%;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script>
<title>OpenLayers example</title>
</head>
<body>
<h2>My Map</h2>
<div id="map" class="map"></div>
<input type="button" value="显示重庆" onclick="showChongQing()">
<script type="text/javascript">
var map = new ol.Map({
layers: [
new ol.layer.Tile({source: new ol.source.OSM()})
],
view: new ol.View({
// 设置为地图中心
center: [106.71, 26.63],
projection: 'EPSG:4326',
zoom: 10
}),
target: 'map'
});
function showChongQing() {
// 让地图最大化完全地显示区域[106,26.5,106.14,30.74]
map.getView().fit([106,26.5,106.14,30.74], map.getSize());
}
</script>
</body>
</html>
##调动地图
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css">
<style>
.map {
height: 200px;
width: 100%;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script>
<title>OpenLayers example</title>
</head>
<body>
<h2>My Map</h2>
<p>地图1</p>
<div id="map1" class="map"></div>
<p>地图2</p>
<div id="map2" class="map"></div>
<button onclick="swapMap()">地图交换</button>
<script type="text/javascript">
// 创建一个视图
// 创建第一个地图
var map1 = new ol.Map({
layers: [
new ol.layer.Tile({source: new ol.source.OSM()})
],
view: new ol.View({
center: [0, 0],
zoom: 2
}),
target: 'map1'
});
// 创建第二个地图
var map2 = new ol.Map({
layers: [
new ol.layer.Tile({source: new ol.source.OSM()})
],
view: new ol.View({
center: [0, 0],
zoom: 2
}),
target: 'map2'
});
function swapMap() {
// 改变两个地图的容器
map1.setTarget('map2');
map2.setTarget('map1');
}
</script>
</body>
</html>
自定义控件
于 2019-12-25 17:56:02 首次发布