如何在一个HTML页面上显示多个地图

3

我对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>

我知道可以使用数组减少变量来制作地图,但现在我只关心让这些多个地图正常工作。稍后再考虑代码的美观性。


请参见 https://dev59.com/oloV5IYBdhLWcg3wc-Ym#36257493。 - ghybs
谢谢您提供的链接,它指向与下面的答案相同的方向。 - Nostromo
1个回答

5

有一种解决地图无效的方法。当你切换选项卡时,你可以添加正确的方法:

        function openPage(pageName, elmnt) {
            ...
            setTimeout(function(){
                mapAsia.invalidateSize();
                mapAfrica.invalidateSize();
            }, 0);
        }

使用setTimeout后,每次切换标签页都会刷新地图。当然,您可以使其更智能,根据所点击的标签页而不是所有标签页来刷新所需的地图。 更新后的fiddle链接:https://jsfiddle.net/kev7m4d3/1/ 请记住,这只是一个解决方法,可能还有其他解决方案。
引用: invalidateSize(animate)-检查地图容器大小是否发生变化,并在如此时更新地图 - 在动态更改地图大小后调用它,也默认进行平移动画。
这可能是必要的,因为在使用选项卡时,地图被隐藏,无法正确计算自己的大小,另一种解决方案可能是例如在点击适当的选项卡后初始化地图,并且每次地图可见时都会初始化地图和适当的空间。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接