我提到了以下SO问题,以使用javascript(html2canvas)捕获谷歌地图的屏幕截图。 feedback.js,screen-capturing-using-html2canvas,create-screen-shot,capture-div-into-image,html2canvas-github。
我使用静态地图来获取图像,但我有近150个标记,并且URL的总长度超出了2048个字符限制。
我能够在javascript中使用事件监听器捕获屏幕截图。我用来捕获图像的代码如下。我尝试使用window.save,但html2canvas.js中没有这样的方法。有关如何将图像保存到本地文件系统的任何建议吗?
function takeImage() {
html2canvas($("#map-canvas"), {
onrendered: function( canvas ) {
var img = canvas.toDataURL("image/png")
window.open(img);
//window.save(img); /*does not work.*/
}
});
}
事件监听器:
google.maps.event.addListener(map, 'tilesloaded', function() {
takeImage();
});
答案 0 :(得分:1)
我遇到了IE9的问题,在Chrome中,有些图片已下载,但已损坏。我发现IE9的唯一解决方案是在新选项卡上打开图像,然后我可以右键单击并另存为。 我使用html2canvas下载了div区域(地图的容器)作为图像的下一个代码。
function download_image() {
if($.browser.safari) {// Fix for Chrome
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([$("#div_id")[0]], {
logging: false,
useCORS: true,
onrendered: function (canvas) {
if ($("#downloadimg").length > 0)
$("#downloadimg").remove();
var fileName = "file_name.png";
if (/\bMSIE|Trident\b/.test(navigator.userAgent) && $.browser.version.match(/9.0|10.0|11.0/)) {//Only for IE 9, 10 and 11
download_image_IE(canvas, fileName);
}
else {
$("body").append("<a id='downloadimg' download='" + fileName + "' href='" + canvas.toDataURL("image/png").replace("image/png", "image/octet-stream") + "'><a/>");
}
if ($("#downloadimg").length > 0)
$("#downloadimg")[0].click();
if($.browser.safari) {// Fix for Chrome
$(".gm-style>div:first>div").css({
left:0,
top:0,
"transform":transform
});
}
}
});
}
function download_image_IE(canvas, filename) {
if ($.browser.version.match(/9.0/)){ //Only for IE9
var w = window.open();
$(w.document.body).html('<img src="'+ canvas.toDataURL() +'"/>');
}
else{
var image = canvas.toDataURL();
image = image.substring(22); // remove data stuff
var byteString = atob(image);
var buffer = new ArrayBuffer(byteString.length);
var intArray = new Uint8Array(buffer);
for (var i = 0; i < byteString.length; i++) {
intArray[i] = byteString.charCodeAt(i);
}
var blob = new Blob([buffer], { type: "image/png" });
window.navigator.msSaveOrOpenBlob(blob, filename);
}
}