使用html2canvas捕获图像后无法加载谷歌地图

时间:2017-12-19 06:27:31

标签: javascript html css web html2canvas

我想使用html2canvas捕获谷歌地图图像,但问题是当我捕获图像时除了方向我看不到谷歌地图。有人可以帮我解决这个问题。下面是参考图片,我附上代码。

function getScreenShot() {

      html2canvas(document.querySelector("#map")).then(canvas => {

      var img = canvas.toDataURL();
      window.open(img);
      height=height,
      width=width

  });
 }

Before capturing image.

After capturing image.

1 个答案:

答案 0 :(得分:0)

为什么不使用谷歌地图的 staticmap 概念?使用静态地图,您可以轻松创建图像。您可以看到以下功能。

  function getScreenShot() {
    //google static map url
    var staticM_URL = "https://maps.googleapis.com/maps/api/staticmap";

    //Set the Google Map Center.
    staticM_URL += "?center=" + mapOptions.center.lat() + "," + mapOptions.center.lng();

    staticM_URL  += "&size=520x650";  //Set the Google Map Size. 

    staticM_URL  += "&zoom=" + mapOptions.zoom;  //Set the Google Map Zoom. 

    staticM_URL  += "&maptype=" + mapOptions.mapTypeId;//Set the Google Map Type.

    //Loop and add Markers.
    for (var i = 0; i < markers.length; i++) {
        staticM_URL  += "&markers=color:red|" + markers[i].lat + "," + markers[i].lng;
    }

   window.open(staticM_URL);
}

一些可以帮助您的参考网址。 https://staticmapmaker.com/google/ https://www.aspsnippets.com/Articles/Take-Screenshot-Snapshot-of-Google-Maps-using-JavaScript.aspx