代码之家  ›  专栏  ›  技术社区  ›  Nostromo

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

  •  3
  • Nostromo  · 技术社区  · 7 年前

    我对HTML、CSS、JavaScript和传单很陌生。

    我尝试创建一个具有多个选项卡的HTML文件,在每个选项卡上,我都希望显示一个不同的映射(左侧),其中包含映射显示内容(右侧)的信息。

    我发现 this 关于如何做选项卡部分的链接,我已经在地图部分有了一个工作示例,但是HTML文件上只有一个地图。

    现在我试着把这两件事结合起来,但只有一张地图工作正常(第一张),其他地图只显示灰色,可能在左上角有一小块地图。但即使我试图平移“破碎”的地图,也只显示部分瓷砖。

    有人能告诉我怎么做吗?一个带有传单的HTML文件上甚至可以有多个地图吗?

    这是到目前为止我的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>
    

    我知道我可以用较少的变量使用数组来做映射,但是现在我只想让这些多个映射工作。我以后会关心代码的漂亮。

    1 回复  |  直到 7 年前
        1
  •  4
  •   Łukasz Blaszyński    7 年前

    有解决方案可以使地图的大小无效。您可以在更改选项卡时添加正确的方法:

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

    有了这个设置,您的地图将在每次标签更改后刷新,当然,您可以更加智能地根据单击的标签刷新只需要的地图,而不是全部。 更新小提琴: https://jsfiddle.net/kev7m4d3/1/

    请记住,这是一个解决方法,可能还有其他解决方法。

    无效大小(动画)-检查映射容器大小 更改并更新地图(如果有)-在更改 动态映射大小,默认情况下也为平移设置动画。

    这很可能是需要的,因为当使用选项卡映射被隐藏时,不能正确计算自己的大小,另一种解决方案可能是,例如,单击适当的选项卡后初始化映射,每次当选项卡和适当的映射空间可见时初始化映射。