【问题标题】:html2canvas google maps market labels disappear - z-index problem?html2canvas 谷歌地图标记标签消失 - z-index 问题?
【发布时间】:2019-12-13 17:45:09
【问题描述】:

我在从谷歌地图获取 html2canvas 生成的图像上的标签时遇到问题。我尝试使用最新的 html2canvas 库,因为图像质量要好得多,但标签会出现问题。我的搜索导致我出现 z-index 问题,但我自己无法处理。请指教

这是一个有效的代码:https://jsfiddle.net/84mcwt17/9/

有问题的图片https://pasteboard.co/IJKZ7Vf.png

html代码:

<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
    async defer></script>

 <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js">
</script>
<div id="map" ></div>

<br/>
<input type="button" id="btnSave" value="Save PNG"/>

  <div id="img-out"></div>

JS代码:

    $(function() { 
    var map;
    var latlng = new google.maps.LatLng(49.241943, -122.889318);

    var myOptions = {
        zoom: 12,
        center: latlng,
        fullscreenControl: false,
        scale:1,
        disableDefaultUI: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map"), myOptions); 
    var positions = [
       new google.maps.LatLng(49.241, -122.899318),
       new google.maps.LatLng(49.242, -122.889318),
       new google.maps.LatLng(49.243, -122.909318)
       ]
    for (var pos of positions){
        let marker = new google.maps.Marker({
          position: pos,
          map: map,
          optimized: false,
          draggable:true,
          label:'1',
        });
            marker.setMap(map);
        }
    $("#btnSave").click(function() { 
              addMapBase64();
            }); });
  function addMapBase64() {
        var target = document.getElementById("map");
        html2canvas(target, {
                useCORS: true
            })
            .then(function (canvas) {
                var canvasImg = canvas.toDataURL("image/png");
                console.log(canvasImg);
                $('#img-out').html('<img src="' + canvasImg + '" alt="">');
                document.getElementById("id_map_base64").value = canvas.toDataURL('image/png');

            })
            .catch(function (err) {
                console.log(err);
            });

    }

css

#map {
    width: 300px;
    height: 200px;
}

【问题讨论】:

  • 添加 'zIndex: 0' 到标记解决了这个问题

标签: maps html2canvas


【解决方案1】:

将'zIndex: 0'添加到标记解决了这个问题

let marker = new google.maps.Marker({
          position: pos,
          map: map,
          optimized: false,
          draggable:true,
          label:'1',
          zIndex: 0
        });

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 2012-12-19
    • 2013-11-18
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    相关资源
    最近更新 更多