简体   繁体   English

Google Maps Javascript API - 图例最大高度

[英]Google Maps Javascript API - Legend Max Height

I'm displaying a map using Google Maps Javascript API and have created a custom legend to toggle certain map paths/markers on and off.我正在使用 Google Maps Javascript API 显示地图,并创建了一个自定义图例来打开和关闭某些地图路径/标记。 The data in the legend is loaded by PHP using entries in a database, so I don't have control over how many checkboxes will be present.图例中的数据是由 PHP 使用数据库中的条目加载的,因此我无法控制将出现多少个复选框。 At the moment, the legend div expands as required to accommodate however many checkboxes are required, but I'm looking to stop it from expanding beyond the confines of the map.目前,图例 div 会根据需要扩展以适应需要多少复选框,但我希望阻止它扩展到地图范围之外。

At the moment, my legend displays as below:目前,我的图例显示如下: 地图

And when I move the bottom of the browser up to reduce the screen height, it draws the div beyond the bottom of the screen:当我向上移动浏览器底部以降低屏幕高度时,它会绘制超出屏幕底部的 div: 地图剪裁

Does anyone know of a way to tell the div not to expand vertically beyond the edge of the map?有没有人知道告诉 div 不要垂直扩展到地图边缘之外的方法? I could obviously set a max-height value based on the height of the map, minus the top position of the legend div, but I'm looking for something a little more native, possibly along the lines of map.controls[google.maps.ControlPosition.LEFT_TOP].push(document.getElementById('mapLegend'));我显然可以根据地图的高度设置一个最大高度值,减去图例 div 的顶部位置,但我正在寻找更原生的东西,可能沿着map.controls[google.maps.ControlPosition.LEFT_TOP].push(document.getElementById('mapLegend')); but for setting the bottom most value?但是为了设置最低值? That way, the map can adjust as required even on mobile or small screens (like in the second screenshot where the Google logo displays over the legend).这样,即使在移动设备或小屏幕上,地图也可以根据需要进行调整(例如在第二个屏幕截图中,Google 徽标显示在图例上方)。

I know this is a very open ended question but I would appreciate any help or pointers anyone can give.我知道这是一个非常开放的问题,但我很感激任何人可以提供的任何帮助或指示。

Thanks in advance.提前致谢。

I ended up using the CSS calc function to manually set the max height of the legend depending on the size of the screen.我最终使用 CSS calc函数根据屏幕大小手动设置图例的最大高度。

    .mapLegend {
        height: auto;
        max-height: calc(100vh - 175px);       /* Top bar, plus height of Google logo at bottom, plus absolute top position of Legend div, plus buffer */
    }

    @media (max-height: 269px) {
        .mapLegend {
            height: auto;
            max-height: calc(100vh - 120px);    /* When screen <270px tall, some Google Maps controls are hidden, so legend moves up, so can expand further down the bottom */
        }
    }

By setting the height to auto, it will only be as big as it needs to be to accommodate as many checkboxes as are necessary, and the max-height stops the overflow from extending beyond the bottom of the screen.通过将高度设置为自动,它只会根据需要容纳尽可能多的复选框,并且最大高度会阻止溢出超出屏幕底部。 Note that on small screens (or when a tall screen is dragged up to reduce it's height), the Google Maps UI changes to hide some non-essential controls, so I added the screen height breakpoint to cater for this.请注意,在小屏幕上(或当向上拖动高屏幕以降低其高度时),Google 地图 UI 会更改以隐藏一些非必要控件,因此我添加了屏幕高度断点来满足此要求。

Hope this helps someone.希望这可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM