Skip to content

Instantly share code, notes, and snippets.

@jawsthegame
Last active May 1, 2021 15:56
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jawsthegame/6801698 to your computer and use it in GitHub Desktop.
Save jawsthegame/6801698 to your computer and use it in GitHub Desktop.
var printMap = function(map) {
map.setOptions({
mapTypeControl: false,
zoomControl: false,
streetViewControl: false,
panControl: false
});
var popUpAndPrint = function() {
dataUrl = [];
$('#map-canvas canvas').filter(function() {
dataUrl.push(this.toDataURL("image/png"));
})
var container = document.getElementById('map-canvas');
var clone = container.clone();
var width = container.clientWidth
var height = container.clientHeight
$(clone).find('canvas').each(function(i, item) {
$(item).replaceWith(
$('<img>')
.attr('src', dataUrl[i]))
.css('position', 'absolute')
.css('left', '0')
.css('top', '0')
.css('width', width + 'px')
.css('height', height + 'px');
});
var printWindow = window.open('', 'PrintMap',
'width=' + width + ',height=' + height);
printWindow.document.writeln($(clone).html());
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
map.setOptions({
mapTypeControl: true,
zoomControl: true,
streetViewControl: true,
panControl: true
});
};
setTimeout(popUpAndPrint, 500);
};
@L0g1k
Copy link

L0g1k commented Jan 31, 2014

I think you mean

clone = $(container).clone()?

@abbas-bafekr
Copy link

i think makes better with this line
$(".gmnoprint", clone).remove();

@fayefaye02
Copy link

Thanks for this! In my case, I created a div to contain the map plus directions and used that in place of 'map-canvas' above. It opens without all of the extra garbage and is perfect. Thanks again!

@myGitHubForum
Copy link

Is there any demo or example plunkr or something? THanks.

@rbsandoval
Copy link

any demo?

@miteshgpatel
Copy link

miteshgpatel commented Apr 17, 2017

dataUrl.push(this.toDataURL("image/png"));
is throwing SecurityError: The operation is insecure. any fix for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment