html2canvas does not work with Google Maps Pan
Asked Answered
F

5

23

I'm using html2canvas to save my online map as an image (See the Save as Image link). I've tried it in Firefox, Chrome and Opera.

It tends to work more often if you do not alter the default map. If you zoom and then pan the map, it is less likely to work. The map will pan, but html2canvas will use the old center point and map bounds. And html2canvas will fail to load map tiles for the new map bounds.

The map pans correctly, but html2canvas uses the old center point and map bounds. Why is this?

To support getting images from different domains I have the setting:

useCors: true;

I have tried the following solutions

-Manually changing the map type. Sometimes this fixes it.

-Triggering the browser resize event - not useful.

-Using setTimeout() to wait 2000 ms to ensure the tiles are loaded - not useful

-Using a proxy (html2canvas_proxy_php.php) - not useful

-Using the google maps idle event to wait for the map to be idle before saving - not useful

Futurism answered 4/6, 2014 at 20:16 Comment(8)
I might be running into a problem with the Same Origin Policy. But then I'm not sure why this would be sporadic. I'm accessing images and javascript from different servers.Futurism
If I add the "allowTaint:true" parameter, then it saves my image map layer but doesn't save the underlying map layer.Futurism
I might need to wait for the map pan to finish loading image tiles, before I try to save the image.Futurism
What's up with the down-voting? Is it because the issue is sporadic?Futurism
did you try nihilogic.dk/labs/canvas2image canvas2image?Merth
You should provide your script for image saving so it can be debugged. I think the problem is that you have set a specific size for the image? For me it always comes up saying the pixel size is "Natural: 1351 × 579 pixels"Holo
Eikoc: it's javascript - so you can see it. I don't specify an image size. The image size is based on the browser window.Futurism
Changing the map type sometimes helps. I think there is a problem with image caching or loading. Can I make Google reload all the map tiles? This had some ideas on tile reloading: #12174008Futurism
A
66

Apparently, the problem seems to stem from html2canvas not being able to render css transforms, at least in chrome (I could only reproduce the problem in chrome, on OSX). The container that holds the tiles, is translated using -webkit-transform. So what we could do is to grab the values that the container is shifted, remove the transform, assign left and top from the values we got off transform then use html2canvas. Then so the map doesn't break, we reset the map's css values when html2canvas is done.

So I pasted this into the javascript console at your site and at it seemed to work

//get transform value
var transform=$(".gm-style>div:first>div").css("transform")
var comp=transform.split(",") //split up the transform matrix
var mapleft=parseFloat(comp[4]) //get left value
var maptop=parseFloat(comp[5])  //get top value
$(".gm-style>div:first>div").css({ //get the map container. not sure if stable
  "transform":"none",
  "left":mapleft,
  "top":maptop,
})
html2canvas($('#map-canvas'),
{
  useCORS: true,
  onrendered: function(canvas)
  {
    var dataUrl= canvas.toDataURL('image/png');
    location.href=dataUrl //for testing I never get window.open to work
    $(".gm-style>div:first>div").css({
      left:0,
      top:0,
      "transform":transform
    })
  }
});
Annalee answered 18/6, 2014 at 9:21 Comment(9)
Brilliant! I got it to save the image in a new window too. Solution works in Firefox, Chrome, and Opera.Futurism
Worked great for me too on Ionic.Olcott
can you please provide a jsfiddle. I works partially for me. It trims the map. Thank you.Parmenides
This is working for mapview, any idea why streetview is broken?Fermi
@Fermi StreetView by default uses webgl, then 3d transforms on images which html2canvas can't process. Easy fix is to set streetview mode/rendering to html4. Example here: jsfiddle.net/L0c3mL53Annalee
@Annalee thank you for this. I will try this approach. In addition to the problem with Googlemap, I found in IE 9 ,10, in ROADMAP mode using your solution, html2canvas returns only partial view: github.com/niklasvh/html2canvas/issues/345. Do you have any idea? I have the same log as this guy: #32175748Fermi
@Annalee here is the screenshots of what I got from IE9 IE10---- github.com/niklasvh/html2canvas/issues/750Fermi
@mfirdaus, i got undefined in transform,can u tell me what wrong i m doing ?Damek
This used to work up to version (v=3.30), now it is not working anymore since the div scructure changed. Moreover, it is also using translate. Has anyone figured how to use the same hack idea on version 3.32?Scriber
C
8

After a Google Maps update the solution of mfirdaus stop working, the new solution is this:

var transform = $(".gm-style>div:first>div:first>div:last>div").css("transform")
var comp = transform.split(",") //split up the transform matrix
var mapleft = parseFloat(comp[4]) //get left value
var maptop = parseFloat(comp[5])  //get top value
$(".gm-style>div:first>div:first>div:last>div").css({ //get the map container. not sure if stable
  "transform": "none",
  "left": mapleft,
  "top": maptop,
})

is the same but u need to change de selector from

.gm-style>div:first>div

to

.gm-style>div:first>div:first>div:last>div

Hands up 🙂

Corporeity answered 19/6, 2018 at 22:0 Comment(6)
cannot read split of undefined with your above codeDees
What is the div we are looking for? I'm sure this will be different for people who use map controls or not. If I knew what div I'm suppose to be looking for I could try to find it in the console.Dees
I think I've tried just about every nested div container from gm-style down with no luck with this code.Dees
also getting in chrome "Unable to clone WebGL context as it has preserveDrawingBuffer=false". It saves the image but grey background still.Dees
Actually found out the issue for me was that I was using a custom map id which was set as a vector type map, had to change it to raster and now its working.Dees
This solution does not work anymore; please check and update it.Pieeyed
W
5

In my case i just allowed Cross Origin Resource Sharing (CORS) in the html2Canvas configuration and it worked for me.

useCORS:true,

For more info you can refer to the html2Canvas Documentation: http://html2canvas.hertzen.com/configuration

Weave answered 14/2, 2019 at 6:49 Comment(1)
Does not work. Make sure your answer is up to date.Pieeyed
D
2

I have the same problem, but I used Leaflet Map instead of Google Map.

The code is below

var transform=$(".leaflet-map-pane").css("transform");
if (transform) {
    var c = transform.split(",");
    var d = parseFloat(c[4]);
    var h = parseFloat(c[5]);
    $(".leaflet-map-pane").css({
        "transform": "none",
        "left": d,
        "top": h
    })
}           


html2canvas(document.body).then(function(canvas){
    $(".leaflet-map-pane").css({
        left: 0,
        top: 0,
        "transform": transform
    })
   }
// Here is used html2canvas 1.0.0-alpha.9
Damek answered 7/2, 2018 at 9:6 Comment(1)
hello, I have this problem with Leaflet Maps and wkhtmltopdf, do you know how to solve?Goodlooking
Z
0

Use this code for updated Google Maps.

  // @ts-ignore html2canvas defined via script
  html2canvas($('#shareScreen')[0], {
    useCORS: true,
    allowTaint: false,
    backgroundColor: null,
    ignoreElements: (node) => {
      return node.nodeName === 'IFRAME';
    }
  }).then((canvas) => {
    var dataUrl = canvas.toDataURL('image/png');
    console.log(dataUrl)
    this.shareLoader = false;
  });
Zooid answered 30/8, 2021 at 6:51 Comment(1)
Does not work, please don't just write things to get more views; check them out first.Pieeyed

© 2022 - 2024 — McMap. All rights reserved.