我对html、css、javascript和leaflet都比较陌生。
我尝试创建一个包含多个选项卡的html文件,在每个选项卡上,我想展示不同的地图(左侧),并显示有关地图所显示内容的信息(右侧)。
我在这个链接上找到了如何制作选项卡部分的方法,并且我已经有一个在html文件中只有一个地图的工作示例。
现在我尝试将这两个东西结合起来,但只有一个地图可以正常工作(第一个),其他地图只显示灰色,可能在左上角只有一小部分地图。即使我尝试拖动“损坏”的地图,也只显示部分瓷砖。
请问有人能给我提示如何解决吗?在一个html文件中使用leaflet可以有多个地图吗?
这是我的html文件,包括html、css和javascript。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin=""></script>
<style>
/* Set height of body and the document to 100% to enable "full page tabs" */
body, html {
height: 100%;
margin: 0;
font-family: sans-serif;
}
/* Style tab links */
.tablink {
background-color: #9f9f9f;
color: black;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 5px;
font-size: 16px;
width: 33.33%;
}
.tablink:hover {
background-color: #7f7f7f;
}
/* Style the tab content (and add height:100% for full page content) */
.tabcontent {
color: black;
background-color: white;
display: none;
padding: 15px 5px;
height: 95%;
}
.map {
float: left;
width: 34.8%;
height: 100%;
border-radius: 5px 5px 0px 0px;
border-width: 1px;
border-style: dotted;
border-color: #000000;
}
</style>
</head>
<body>
<button class="tablink" onclick="openPage('Europe', this)" id="defaultOpen">Europe</button>
<button class="tablink" onclick="openPage('Asia', this)">Asia</button>
<button class="tablink" onclick="openPage('Africa', this)">Africa</button>
<div id="Europe" class="tabcontent">
<div id="mapEurope" class="map"></div>
</div>
<div id="Asia" class="tabcontent">
<div id="mapAsia" class="map"></div>
</div>
<div id="Africa" class="tabcontent">
<div id="mapAfrica" class="map"></div>
</div>
<script>
function openPage(pageName, elmnt) {
// Hide all elements with class="tabcontent" by default */
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Remove the background color of all tablinks/buttons
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = "";
}
// Show the specific tab content
document.getElementById(pageName).style.display = "block";
// Add the specific color to the button used to open the tab content
elmnt.style.backgroundColor = "#ffffff";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
//Create maps
var centerLatEurope = 55.0;
var centerLngEurope = 18.0;
var initialZoomEurope = 4;
var mapEurope = L.map('mapEurope', {
center: [centerLatEurope, centerLngEurope],
zoom: initialZoomEurope
});
//set one map tiles source
googleStreetsEurope = L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}', {
attribution: '<a href="https://www.google.com/maps">Google Maps</a>',
maxZoom: 20,
subdomains:['mt0','mt1','mt2','mt3']
});
googleStreetsEurope.addTo(mapEurope);
var centerLatAsia = 49.0;
var centerLngAsia = 88.0;
var initialZoomAsia = 2;
var mapAsia = L.map('mapAsia', {
center: [centerLatAsia, centerLngAsia],
zoom: initialZoomAsia
});
//set one map tiles source
googleStreetsAsia = L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}', {
attribution: '<a href="https://www.google.com/maps">Google Maps</a>',
maxZoom: 20,
subdomains:['mt0','mt1','mt2','mt3']
});
googleStreetsAsia.addTo(mapAsia);
var centerLatAfrica = 0.0;
var centerLngAfrica = 18.0;
var initialZoomAfrica = 4;
var mapAfrica = L.map('mapAfrica', {
center: [centerLatAfrica, centerLngAfrica],
zoom: initialZoomAfrica
});
//set one map tiles source
googleStreetsAfrica = L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}', {
attribution: '<a href="https://www.google.com/maps">Google Maps</a>',
maxZoom: 20,
subdomains:['mt0','mt1','mt2','mt3']
});
googleStreetsAfrica.addTo(mapAfrica);
</script>
</body>
</html>
我知道可以使用数组减少变量来制作地图,但现在我只关心让这些多个地图正常工作。稍后再考虑代码的美观性。