admin管理员组

文章数量:1331616

I am trying to print a map I have created with Google Maps API v3. The map contains a polygon. I am using the pop-up method to try to print it:

var gmapPrint = function() {
  var content = window.document.getElementById('map-canvas') // get you map details
  var newWindow = window.open(); // open a new window
  newWindow.document.write(content.innerHTML); // write the map into the new window
  newWindow.print(); // print the new window
} 

The map prints just fine, but the polygon is missing. At first, it seemed as though the print dialog was popping up before the polygon was drawn on the map, so I added a setTimeout. This did not fix the issue. I have also tried using the static maps API, but the number of points in the boundary of my polygon exceed the maximum URL size, even when encoded.

Has anyone had any success with this?

I am trying to print a map I have created with Google Maps API v3. The map contains a polygon. I am using the pop-up method to try to print it:

var gmapPrint = function() {
  var content = window.document.getElementById('map-canvas') // get you map details
  var newWindow = window.open(); // open a new window
  newWindow.document.write(content.innerHTML); // write the map into the new window
  newWindow.print(); // print the new window
} 

The map prints just fine, but the polygon is missing. At first, it seemed as though the print dialog was popping up before the polygon was drawn on the map, so I added a setTimeout. This did not fix the issue. I have also tried using the static maps API, but the number of points in the boundary of my polygon exceed the maximum URL size, even when encoded.

Has anyone had any success with this?

Share Improve this question edited Oct 2, 2013 at 2:03 Brett 2,3991 gold badge18 silver badges24 bronze badges asked Oct 2, 2013 at 1:53 JawsTheGameJawsTheGame 2331 gold badge4 silver badges9 bronze badges 3
  • Can you just create a copy of your page without anything else on it except for the map and the scripts used to create the map? Then just open that page in the new window. If you need to get sneaky you could pass url parameters to the new page to capture things like the current map zoom level or center point and possibly other things as well. – astupidname Commented Oct 2, 2013 at 6:09
  • A potential solution is reported here. It's not generalised - (a) as written, it will print only the map, not the whole page, (b) you will need to adjust dimensions to suit your map. – Beetroot-Beetroot Commented Oct 2, 2013 at 7:31
  • Oh yes, the solution uses jQuery, though the author forgot to mention it. – Beetroot-Beetroot Commented Oct 2, 2013 at 7:44
Add a ment  | 

1 Answer 1

Reset to default 6

Ok, so Beetroot-Beetroot's ment got me on the right track. I had seen that post earlier and gave it a quick shot without much luck, but now I've got it working.

Here is a snippet to print a google map without the controls visible.

https://gist.github./jawsthegame/6801698

本文标签: javascriptPrint a Google Maps API v3 map with polygonStack Overflow