Google Maps API V3 Printing Maps

前端 未结 5 1634
北海茫月
北海茫月 2020-12-09 14:13

I am looking for a method to efficiently print Google maps that have been implemented on a site using the google maps api v3.

I have seen some people using just the

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 14:19

    You can use html2canvas.js to convert map div to canvas element, then you can print it as image. Below code works perfect for me:

    HTML

    JavaScript

    var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 13,
                center: defLocation,
                mapTypeId: 'satellite',
                streetViewControl: false
            });      
    $(document).on('click', '.printBtn', function () {
                map.setOptions({
                    mapTypeControl: false,
                    zoomControl: false,
                    streetViewControl: false,
                    panControl: false
                });
                var printWin = window.open('', '', 'width=1000,height=700');
                var windowContent = '';
    
                html2canvas($("#map"), {
                    useCORS: true,
                    onrendered: function (canvas) {
                        windowContent += ''
                        windowContent += 'Print canvas';
                        windowContent += ''
                        windowContent += '';
                        windowContent += '';
                        windowContent += '';
                        printWin.document.open();
                        printWin.document.write(windowContent);
                        printWin.document.close();
                        printWin.focus();
                        setTimeout(function () { printWin.print(); printWin.close(); }, 500);
    
                        map.setOptions({
                            mapTypeControl: true,
                            zoomControl: true,
                            streetViewControl: true,
                            panControl: true
                        });
                    }
                });
            });
    

提交回复
热议问题