Print button for Google Map API
You can easily print google map from maps.google.com. But what about printing the map from gmap api you use on your own website? One way is to print the whole page which is not a good idea. Users want to print only the location of your business in a larger size for later purposes. Google map api does not provide any function to print the map or view it in printer friendly mode.
The only solution I found is using a simple javascript code for this purpose. At first place an image/text/button anywhere (obviously easily available to visitors) on the page that contains your map api. I prefer to use a small printer icon image. Apply onclick(inline or unobtrusive) event to the button to popup a page say print.html.
<img src="images/print.png" class="print" alt="print" title="print" onclick="window.open('print.html')" />
Now create an empty html document, name it print.html and put it in the root folder of your site. Place this simple javascript code inside body tag of print.html
<script language="javascript">
var contents = window.opener.document.getElementById("map_container");
document.write(contents.innerHTML);
window.print();
</script>
Replace the “map_container” with the #id you are using for your map container. That’s all, you are done!
This method is good enough for printing static google map layout. But you can not print driving direction as that line is drawn using canvas element. In fact you can not print the exact driving direction even from maps.google.com.
This is more or less a better solution for printing the map from your own website’s gmap api. Please lemme know if you know any smarter way to do it.
Related posts:
- A pair of printer driver updates released by Apple
- SEO tips: Reduce your unwanted external links using jQuery
- How to get currently logged on windows username in PHP and Javascript
7 Comments to “Print button for Google Map API”
Leave a Reply



My Elance Profile
I searched for a post like this. It’s helped me, thanks very much – but I have a problem. If I draw polylines to the map it won’t be printed. Could you help me?
Sorry @kree, you can not get the lines in this way, cos the lines are drawn on canvas object via javascript and when you simply get the inner html you won’t get the canvas drawings.
To do this you have to print the map directly from google map site.
Thanks @adnan. I have to search another way then…
Seems that the Static maps API provides a simpler way to get a printable image :
http://code.google.com/intl/fr/apis/maps/documentation/staticmaps/
Thank you @prisme for the info.
That worked perfectly – thanks so much! been looking for a while for a way to do this
thanks for sharing :)
you are most welcome @Markgt